PHP supports eight basic data types. For more information about php learning, see.
Four scalar types:
- Boolean (boolean)
- Integer)
- Float (float, also called double)
- String (string)
Two composite types:
There are two special types:
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, use var_dump ().
If you just want to get an easy-to-understand expression of the type for debugging, use gettype (). To view a type,NoUse gettype () andIs_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 or settype () function for it.
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.