Four scalar types:
Two composite types:
Finally there are two special types:
To ensure the readability of the code, this manual also introduces some pseudo-types:
and pseudo variables $....
You may also read some references to the "double" type. In fact, double and float are the same, and for some historical reasons, these two names existed 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 var_dump().
If you just want to get an easy-to-read type expression for debugging, use gettype(). To check a certain type, don’t use gettype(), but use is_type function. Here are some examples:
Copy code The code is as follows:
$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";
}
?>
If you want to force a variable to a certain type, you can use cast or settype () function.
Note that variables will exhibit different values on specific occasions depending on their type at the time. See Type Tricks for more information. Additionally, you can refer to the PHP Type Comparison Chart for examples of how different types compare to each other.
The above has introduced a summary of the eight basic data types of PHP in JavaScript data types, including the content of JavaScript data types. I hope it will be helpful to friends who are interested in PHP tutorials.