This article summarizes and analyzes PHP Data types in detail. For more information, see
PHP has a total of 8 data types:
Type name |
Type representation |
Value |
Bool |
Boolean |
True, false |
Integer |
Integer |
-2147483647-2147483648 |
String |
String type |
The string length depends on the machine memory. |
Float |
Floating point type |
Maximum 1.8e308 |
Object |
Object |
Instantiate $ obj = new person () through new (); |
Array |
Array type |
$ Arr = array (1, 2, 3, 4, 5, 6); // A one-dimensional array |
Resourse |
|
|
Null |
Null value |
Null |
Boolean bool:
For other types, we can use (bool) or (boolean) to forcibly convert eg :( bool) 1 = true;
In the following cases, the default value is false for forced conversion:
Conversion |
Result |
Boolean false var_dump (bool) false) |
Bool (false) |
Integer 0 var_dump (bool) 0 ); |
Bool (false) |
Floating point 0.0 var_dump (bool) 0.0 ); |
Bool (false) |
String '0' var_dump (bool) '0 '); |
Bool (false) |
Empty array $ arr = array (); var_dump (bool) $ arr) |
Bool (false) |
Empty objects that do not contain any member variables are used only in PHP4, and true in PHP5 |
Bool (false) |
NULL or the variable var_dump (bool) NULL) that has not been assigned a value) |
Bool (false) |
SimpleXML object generated from an XML document without any tags |
Bool (false) |
String '0. 0' is converted to bool (true)
Note:-1 and other non-zero values (both positive and negative) are true.
Integer:
The integer value range is-2147483647--2147483647. if this value is exceeded, it is automatically converted to float type.
We can use echo PHP_INT_SZIE to output the integer length, which is related to the machine. Echo PHP_INT_MAX outputs the maximum integer value
PHP does not perform an integer division operation. If 1/2 is executed, float 0.5 is generated. if you want to perform the integer division, you can use (int) (1/2) = 0 or round (25/7) = 4.
Forcibly convert to integer (int) or (integer) bool type true to 1, false to 0
Float:
Maximum Value: 1.8e308. what is the minimum value? Please advise
The font length of a floating point number is also related to the machine. it seems that there is no PHP_FLOAT_SIZE. please tell me how to extend it to a floating point number.
String type string:
Four methods for defining strings:
1. single quotes
2. double quotation marks
3. heredoc syntax structure
4. nowdoc syntax structure (after PHP5.3.0)
Single quotes
The single quotation mark defines the original string, and all content in it is processed by the string. if the string contains single quotation marks, it can be escaped \.
Double quotation marks
Strings defined by double quotation marks will parse some special characters (\ n, \ B) and variables
You can place variables in double quotation marks instead of converting them into strings ):
$ Num = 10;
$ Str = "$ num"; // $ str is 10 of the string type
Heredoc syntax structure
< <标示符
String itself
Identifier
The identifier at the end must be at the beginning of a line, and the definition format of the identifier must follow the rules defined by PHP. it can only contain numbers, letters, and underscores, and cannot start with a number underline.
If a line of the ending identifier does not contain any other character, you can add a semicolon after the identifier, and there cannot be tabs or spaces before and after the semicolon; otherwise, PHP will not be able to parse the identifier, the identifier will be searched down. if it is not found before the end of the file, an error will be generated.
Heredoc is a double quotation mark without double quotation marks. it can contain double quotation marks without escaping and can parse special characters and variables.
Nowdoc syntax structure
<'Identifier'
String itself
The start identifier of nowdoc must be enclosed in single quotes. The end identifier and other rules are the same as those of heredoc.
Nowdoc is a single quotes without single quotes. the strings contained in nowdoc are output as they are, and the special characters and variables contained in nowdoc are not parsed.
If double quotation marks contain several situations in array variables
// Define the following array first
The code is as follows:
[Php]
$ Arr = array (
'One' => array (
'Name' => 'jiangtongg ',
'Sex' => 'male'
),
'Two' => 'zhaohaitao ',
'Three '=> 'fanchangfa'
);
The first element in the array is two-dimensional, and the last two are one-dimensional. when we access one-dimensional, the following methods are provided:
The code is as follows:
[Php]
Echo "$ arr [two]" // The key has no single quotation marks.
Echo "$ arr ['two']" // an error occurs when the key has single quotes. if we change it to echo "{$ arr ['two']}", the result can be output correctly.
Echo "{$ arr [two]}" // There are double braces, but the key does not have single quotes. in this case, PHP will first look for the constant banana.
Replacement, because there is no two constant, an error occurs.
It can be seen that when accessing a one-dimensional array, either the key is not enclosed by quotation marks (considering the third case). if it is added, it will be included.
Multi-dimensional array test
The code is as follows:
[Php]
Echo "$ arr [one] [name]"; // The output result is Array [name]. it can be seen that it returns an Array and only resolves one dimension.
Echo "{$ arr ['one'] ['name']}"; // The output result is jiangtong.
The braces key must be enclosed in double quotation marks for multi-dimensional array access.
Array type
As mentioned in the string type, if it is legal to enclose it in braces without the key quotation marks, PHP will first look for a constant named as the key, if yes, it will be replaced. If no, it will generate a warning that the constant cannot be found, which will be processed according to the normal string. Therefore, we recommend that you add single quotation marks.
To convert to an array, use (array) type or array (type). However, if only one value is converted to an array, an array of elements is obtained and the subscript is 0, convert NULL to an array to obtain an empty array.
We can change the value of the array when traversing the array. you can use the reference implementation above PHP5.0.
The code is as follows:
[Php]
$ Arr = array ('A', 'B', 'C', 'D', 'E ');
Foreach ($ arr as & $ value)
{
$ Value = strtoupper ($ value );
Echo $ value;
} // Output result ABCDE
Object Type
To instantiate an object, we use new to add a person class. we can use the following method:
The code is as follows:
[Php]
$ ObjPerson = new person ();
Forced conversion (object ):If an object is converted to an object, it does not change. for any other value, an stdclass object will be instantiated. if this value is NULL, an empty object will be instantiated, if you convert an array to an object, the key of the array is used as the object's attribute. value is the attribute value, and other types of values are named scalar member variables that contain this value.
The code is as follows:
[Php]
$ Arr = array ('one' => 'A', 'two' => 'B ');
$ Obj = (object) $ arr;
Echo $ obj-> one // The output result is;
Note: This is an array composed of keys. if there is no character key array, I don't know how to access it. I want to tell my younger brother. thank you.
For other values
The code is as follows:
[Php]
$ Obj1 = (object) 'Jiang ';
Echo $ obj1-> scalar; // output result jiang
NULL type
Null is case insensitive. The NULL type has only one value, indicating that a variable has no value. in the following three cases, the variable is considered NULL.
1. The value is NULL.
2. not assigned
3. unset ();
PHP type comparison tables
Comparisons of $ x with PHP functions
Expression |
Gettype () |
Empty () |
Is_null () |
Isset () |
Boolean:If ($ x) |
$ X = ""; |
String |
TRUE |
FALSE |
TRUE |
FALSE |
$ X = null |
NULL |
TRUE |
TRUE |
FALSE |
FALSE |
Var $ x; |
NULL |
TRUE |
TRUE |
FALSE |
FALSE |
$ X is undefined |
NULL |
TRUE |
TRUE |
FALSE |
FALSE |
$ X = array (); |
Array |
TRUE |
FALSE |
TRUE |
FALSE |
$ X = false; |
Boolean |
TRUE |
FALSE |
TRUE |
FALSE |
$ X = true; |
Boolean |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = 1; |
Integer |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = 42; |
Integer |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = 0; |
Integer |
TRUE |
FALSE |
TRUE |
FALSE |
$ X =-1; |
Integer |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = "1 "; |
String |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = "0 "; |
String |
TRUE |
FALSE |
TRUE |
FALSE |
$ X = "-1 "; |
String |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = "php "; |
String |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = "true "; |
String |
FALSE |
FALSE |
TRUE |
TRUE |
$ X = "false "; |
String |
FALSE |
FALSE |
TRUE |
TRUE |
Loose comparisons=
|
TRUE |
FALSE |
1 |
0 |
-1 |
"1" |
"0" |
"-1" |
NULL |
Array () |
"Php" |
"" |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
TRUE |
TRUE |
FALSE |
TRUE |
1 |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
0 |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
TRUE |
TRUE |
-1 |
TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
"1" |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
"0" |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
"-1" |
TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
NULL |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
TRUE |
FALSE |
TRUE |
Array () |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
TRUE |
FALSE |
FALSE |
"Php" |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
"" |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
Strict comparisons===
|
TRUE |
FALSE |
1 |
0 |
-1 |
"1" |
"0" |
"-1" |
NULL |
Array () |
"Php" |
"" |
TRUE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
1 |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
0 |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
-1 |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
"1" |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
"0" |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
"-1" |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
NULL |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
Array () |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
"Php" |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
"" |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |