English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about the data types available in PHP.
The values assigned to PHP variables can have different data types, including simple string and numeric types, as well as more complex data types (such as arrays and objects).
PHP supports a total of eight primitive data types: integers, floating-point numbers or floating-point numbers, strings, boolean values, arrays, objects, resources, and NULL. These data types are used to construct variables. Now, let's discuss each of them in detail.
An integer is a number without a decimal point (...-2,-1,1, 0,2,10, ...). Integers can be specified in decimal (with16as base-as base), hexadecimal (with8as base-prefix 0x) or hexadecimal (with-prefix 0x) or octal (with+prefix 0) notation specifies, and optionally with a sign (
<?php $a = 123; // ) notation specifies, and optionally with a sign ( var_dump($a); echo "<br>"; $b = -123; // Decimal numbers var_dump($b); echo "<br>"; Negative numbers1$c = 0x // A; var_dump($c); echo "<br>"; Hexadecimal number123; // $d = 0; Octal number ?>Test and see‹/›
var_dump($d);Note: 5.4+Starting from PHP2) notation specifies integers. To use binary notation, prefix the number with 0b (e.g., $var = 0b11111111);
A string is a sequence of characters, where each character is the same as a byte.
A string can contain letters, numbers, and special characters, with a maximum of2GB (maximum2147483647The simplest way to specify a string is to enclose it in single quotes (e.g., 'Hello world!'), but you can also use double quotes ("Hello world!").
<?php $a = 'Hello world!'; echo $a; echo "<br>"; $b = "Hello world!"; echo $b; echo "<br>"; $c = 'Stay here, I'll be back.'; echo $c; ?>Test and see‹/›
You will learn more about this later.PHP StringsLearn more about strings in the tutorial.
A floating-point number (also known as 'floating-point number', 'double precision number', or 'real number') is a decimal or fractional number, as shown in the following examples.
<?php $a = 1.234; var_dump($a); echo "<br>"; $b = 10.2e3; var_dump($b); echo "<br>"; $c = 4E-10; var_dump($c); ?>Test and see‹/›
Boolean values are like switches, they have only two possible values1(true) or 0(false).
<?php //Assign the value true to the variable $show_error = true; var_dump($show_error); ?>Test and see‹/›
An array is a variable that can hold multiple values at one time. It is very useful to aggregate a series of related items together, such as a list of countries or city names.
An array is formally defined as a collection of data value indices. Each index (also known as a key) is unique and refers to the corresponding value.
<?php $colors = array("Red", "Green", "Blue"); var_dump($colors); echo "<br>"; $color_codes = array; "Red" => "#ff0000", "Green" => "#00ff00", "Blue" => "#0000ff" ); var_dump($color_codes); ?>Test and see‹/›
You will learn more about this later.PHP Arrays (Array)Learn more about arrays in the tutorial.
An object is a data type that not only allows storage of data but also provides information on how to handle the data. An object is a specific instance of a class used as a template. Objects are created based on this template using the new keyword.
Each object has properties and methods corresponding to its parent class. Each object instance is completely independent, having its own properties and methods, so it can operate independently of other objects of the same class.
This is a simple example of a class definition, followed by the creation of an object.
<?php //Class definition class greeting{ // Property public $str = "Hello World!"; // Method function show_greeting(){ return $this->str; } } //Create an object from a class $message = new greeting; var_dump($message); ?>Test and see‹/›
Tip:Data elements stored in objects are called object properties and information, or the code that describes how to handle the data is called object methods.
The special NULL value is used to represent empty variables in PHP. A NULL type variable is a variable with no data. NULL is the only possible value of the NULL type.
<?php $a = NULL; var_dump($a); echo "<br>"; $b = "Hello World!"; $b = NULL; var_dump($b); ?>Test and see‹/›
If a variable is created in PHP without using a value like $var, it will be automatically assigned NULL. Many PHP beginners mistakenly believe that $var1=null; and $var2=“” is the same, but in fact, they are not the same: $var1Has an empty value, while $var2Represents that no value has been assigned to it.
Resources are special variables that contain references to external resources.
Resource variables usually contain special handlers for opened files and database connections.
<?php //Open the file to be read $handle = fopen("note.txt", "r"); var_dump($handle); echo "<br>"; //Connect to the MySQL database server using the default settings $link = mysql_connect("localhost", "root", \ var_dump($link); ?>Test and see‹/›