PHP has a total of 8 data types:
Type name |
Type represents |
Take value |
bool |
Boolean type |
True,false |
Integer |
Integral type |
-2147483647-2147483648 |
String |
String type |
String length depends on machine memory |
Float |
Floating-point type |
Maximum Value 1.8e308 |
Object |
Object |
Instantiate $obj =new person () by new; |
Array |
Array type |
$arr =array (1,2,3,4,5,6);//one-dimensional array |
Resourse |
|
|
Null |
Null value |
Null |
boolean bool:
For other types we can use (bool) or (Boolean) for a cast of eg: (bool) 1=true;
The following conditions are false by default when forced conversion:
Transformation |
Results |
Boolean false Var_dump ((bool) false) |
BOOL (FALSE) |
Integral type 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 PHP5 is true |
BOOL (FALSE) |
NULL or Variable Var_dump ((bool) null) that has not been assigned a value |
BOOL (FALSE) |
SimpleXML object generated from an XML document that does not have any tags (tags) |
BOOL (FALSE) |
The string ' 0.0 ' converts the result to bool (true)
Note: 1 and other non-0 values (either positive or negative) are true
Integer integer:
The range of the integral type is -2147483647--2147483647 and automatically converted to float if the value is exceeded
We can use the echo Php_int_szie to output the integer word length, which is related to the machine. echo Php_int_max outputs the maximum value of an integer
There is no division operation in PHP if the 1/2 will produce a float 0.5 if you want to achieve an integer divide effect, you can use (int) (1/2) =0 or use round (25/7) =4
Cast to Integer (int) or (integer) bool Ture converted to 1, false to 0
floating-point float:
Maximum value range: 1.8e308 What is the minimum value? Ask the expert to tell
The word length of the floating-point number is also related to the machine, as if there is no php_float_size ah, ask the master to tell how to the floating-point numbers long
String Type:
4 ways to define a string:
1. Single quotation mark
2. Double quotes
3.HEREDOC syntax structure
4.NOWDOC syntax structure (after PHP5.3.0)
Single quotation mark
Single quotes define the most primitive strings, all of which are treated as strings, and if the string contains single quotes, you can use the \ Escape
Double quotes
A string defined by double quotes resolves some special characters (\n,\b) and variables
You can place a variable in double quotes instead of converting the variable to a string (string):
$num = 10;
$str = "$num"; $STR is 10 of the string type
HEREDOC Grammatical structure
<<< designator
String itself
Identifiers
The identifier at the end must be at the beginning of a line, and the format of the identifier must be in accordance with the rules defined by PHP, which can contain only numbers, letters, underscores, and cannot begin with a number underline.
End designator which line does not allow other characters, you can add a semicolon after the marker, no tab or space before and after the Shard, or PHP will not be able to parse the identifier, will continue to look up the identifier, if it is still not found before the end of the file will produce an error
Heredoc is a double quote that doesn't use double quotes, which can contain double quotes without escaping, and can parse special characters and variables
NOWDOC Grammatical structure
<<< ' designator '
String itself
The Nowdoc start designator must be enclosed in single quotes, and the end designator and other rules are the same as Heredoc
Nowdoc is a single quote that doesn't use single quotes, Nowdoc contains strings that are output as they are, and the special characters and variables contained in them are not parsed
If you include several cases in the array variable in double quotes
Let's first define the following array
Copy Code code as follows:
[PHP]
$arr =array (
' One ' =>array (
' Name ' => ' Jiangtong ',
' Sex ' => ' man '
),
' Two ' => ' Zhaohaitao ',
' Three ' => ' FANCHANGFA '
);
The first element in the array is two-dimensional, the last two are one-dimensional, and the following are some of the ways we visit one dimension:
Copy Code code as follows:
[PHP]
echo "$arr [two]"//key without single quotes
echo "$arr [' two ']"//key there will be an error in single quotes if we change to echo "{$arr [' two ']}"; You can output the results correctly
Echo ' {$arr [two]} '//has double curly braces, but key does not have single quotes this situation PHP will first look for a constant banana, and some words
Replace, because there is no two constant error <span style= "font-family: ' Courier New '; "> </span>
Visible in the access to a one-dimensional array or no key without quotes (given the third case, AH), add will be {} enclosed, you can not add.
Multidimensional Array test
Copy Code code as follows:
[PHP]
echo "$arr [One][name]"; The output is array[name] visible it returns an array, parsing only one dimension
Echo ' {$arr [' one '] [' name ']} ';//output is Jiangtong
Braces key must be used in double quotes in multidimensional array access
array type
Already mentioned in the string type, enclosed in curly braces, if you do not add key quotes, it is legitimate, then PHP will first look for the name of the key is a constant, some words will be replaced, no words will produce a warning can not find a constant to do the normal string processing, so we recommend that you must add single quotes
Converts an array to use (array) type or array (type), but if the conversion of only one value is a number of groups, you will get a set of elements, and the subscript is 0, and null to convert to a number of arrays will get an empty array
We can change the value of the array while traversing the array, and we can use the reference to implement the PHP5.0
Copy Code code as follows:
[PHP]
$arr =array (' A ', ' B ', ' C ', ' d ', ' e ');
foreach ($arr as & $value)
{
$value =strtoupper ($value);
Echo $value;
}//Output Results ABCDE
Object Type
Instantiating an object we use new to add a person class, and we can do it in the following ways
Copy Code code as follows:
[PHP]
$objPerson =new person ();
cast (object):If you convert an object into an object then it doesn't change, for any other value, a Stdclass object is instantiated, and if the value is null, an empty object is instantiated, and if the array is converted to an object, the key of the array is used as the property of the object, value as the property value, Other type of value is the member variable named scalar that contains the value
Copy Code code as follows:
[PHP]
$arr =array (' One ' => ' a ', ' two ' => ' B ');
$obj = (object) $arr;
echo $obj->one///output result is a;
Note: This is an array of keys, if there is no character key array, I do not know how to access, who know hope to tell the younger brother, thank you.
For other values
Copy Code code as follows:
[PHP]
$obj 1= (object) ' Jiang ';
echo $obj 1->scalar;//output Jiang
NULL NULL type
NULL case insensitive, NULL type has only one value, indicating that a variable has no value, the following three case variables are considered null
1. The assigned value is NULL
2. Has not yet been assigned a value
3. be 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 with = =
|
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 with = =
|
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 |