Variable type changes (juggling)
PHP does not require (or does not support) specifying its variable type in the declared variable; the type of a variable is determined by the context in which the variable is used, that is, if you assign a string to a variable var The value of 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.
$foo = "0"; // $foo is a string with a value of "0" (ASCII 48)
$foo++; // $foo is a string with a value of "1" (ASCII 49)
$foo += 1; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a double (3.3)
$foo = 5 + "10 Little Piggies"; // $ foo is an integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is an integer (15)
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()".
Determine the type of a variable
Because PHP determines the types 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 coercion in PHP is roughly the same as in C language: write the required type in parentheses in front of the variable to be coerced.
$foo = 10; // $foo is an integer
$bar = (double) $foo; // $bar is a double
The following coercion methods are allowed:
(int), (integer) – coerce to integer
(real), (double), (float) – coerce to double
(string) – coerce to string
(array) – coerce to array
(object) – coerce to 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'.
$foo = 1 + "10.5"; // $foo is a double (11.5)
$foo = 1 + "-1.3e3"; // $foo is a double (-1299)
$foo = 1 + "bob -1.3e3"; // $foo is an integer (1)
$foo = 1 + "bob3"; // $foo is an integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is an integer ( 11)
$foo = 1 + "10 Little Piggies"; // $foo is an integer (11);
// This string includes the character 'e'
For more information, see the Unix manual section on strtod(3).