Variable type juggling (juggling) PHP does not require (or does not support) declaring a variable to specify its variable type; the type of a variable is determined by the context in which the variable is used, that is, if you give a variable If var is assigned a string value, var becomes a string variable. If you assign an integer to var, it becomes an integer variable. An example of PHP automatically converting variable types is the addition operator '+'. If any operand is a double, then all operands are evaluated as doubles, and the result is also a double. Otherwise, the operands will be considered to be integers and the result will be an integer. Note that this does not affect the variable type of each operand itself, the only change is how the operands are processed during the calculation. |
|
If you think the last two expressions in the above example look a little strange, please see the "String Conversion" section. If you wish to force a variable to be evaluated as a fixed type, see the "Casting" section. If you wish to change the type of a variable, please see the description of the function "settype()". Determining the type of a variable Because PHP determines the type of variables itself and generally casts them as needed, the type of a particular variable is not always obvious. PHP includes functions to find out the type of this variable. These functions are gettype(), is_long(), is_double(), is_string(), is_array(), and is_object(). Type casting Type casting in PHP is much the same as in C: you will need The type is written in parentheses before the variable to be coerced. |
|
The following coercion methods are allowed: (int), (integer) - coerce to integer (real), (double), (float) - coerce to double precision (string) - coerce to string (array) - coerce to Array (object) – Coerced to an object. Note that tabs and spaces are allowed within parentheses, so the following statements are equivalent: $foo = (int) $bar; $foo = (int) $bar; String conversion When a string is evaluated as a numeric value, its result and type are determined as described below. If this string contains the characters '.', 'e', or 'E', it is treated as a double type variable, otherwise it is treated as an integer. The value of this string is determined by the prefix. If the string begins with any valid numeric data, then the numeric data is the value on which the string is evaluated. Otherwise, the value is zero. Valid numeric data follows the following notation, followed by one or more digits (which may include a decimal point), followed by an optional exponent. The exponent is formed by one or more digits followed by 'e' or 'E'. |
|
For more information, see the Unix manual section on strtod(3). |