Summary of eight basic data types in PHP. Four scalar types: boolean (boolean) integer (integer) float (float type, also known as double) string (string) two composite types: array (array) object (pair
Four scalar types:
- Boolean(Boolean)
- Integer(Integer)
- Float(Float type, also knownDouble)
- String(String)
Two composite types:
- Array(Array)
- Object(Object)
There are two special types:
- Resource(Resource)
- NULL(NULL)
To make sure the code is easy to understand, this manual also introduces some pseudo types:
And pseudo variables$....
You may also read some references about 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 generally not set by the programmer. to be exact, it is determined by PHP at runtime based on the context used by the variable.
Note:To view the value and type of an expression, useVar_dump ().
If you want to use a simple expression for debugging, useGettype (). To view a type,NoUseGettype ()And useIs _TypeFunction. The following are examples:
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 forcibly convert a variable to a certain type, you can use the force conversion orSettype ()Function.
Note that the variable will show different values in specific situations based on its current type. For more information, see Type tricks. In addition, you can refer to the PHP type comparison table to see examples of comparison between different types.
Struct boolean (boolean) integer (integer) float (float type, also known as double) string (string) two composite types: array (array) object (pair...