PHP supports 8 primitive data types.
Four scalar types:
boolean (Boolean)
integer (integer)
float (floating point, also called double)
string (string)
two Specific composite type: aArray For readability, this manual also introduces some pseudo-types:
mixed (mixed type)
number (numeric type)
callback (callback type)
and pseudo-variables $….
You may also read some references to the "double" type. In fact, double and float are the same. For some historical reasons, these two names exist at the same time.
The type of a variable is usually not set by the programmer. Rather, it is determined by PHP at runtime based on the context in which the variable is used.
Note: If you want to check the value and type of an expression, use the var_dump() function. If you just want to get an easy-to-understand type expression for debugging, use the gettype() function. To check a type, don't use gettype(), use the is_type function. Here are some examples:
<?php $a_bool = TRUE; // a boolean $a_str = "foo"; // a string $a_str2 = 'foo'; // a string $an_int = 12; // an integer echo gettype($a_bool); // prints out: boolean echo gettype($a_str); // prints out: string // If this is an integer, increment it by four if (is_int($an_int)) { $an_int += 4; } // If $bool is a string, print it out // (does not print out anything) if (is_string($a_bool)) { echo "String: $a_bool"; } ?>
Note that variables will exhibit different values on specific occasions depending on their type at the time. See Type Conversion Discrimination for more information. In addition, you can also refer to the PHP type comparison table for examples of how different types compare to each other.