$foo = $foo + 1.3; // $foo is now a double (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
If the last two examples above seem odd, see String conversion.
If you wish to force a variable to be evaluated as a certain type, see the section on Type casting. If you
wish to change the type of a variable, see settype.
Type casting
Type casting in PHP works much as it does in C: the name (exp 3pod.com) of the desired type is written in parentheses
before the variable which is to be cast.
$foo = 10; // $foo is an integer
$bar = (double) $foo; // $bar is a double
The casts allowed are:
• (int), (integer) - cast to integer
• (real), (double), (float) - cast to double
• (string) - cast to string
• (array) - cast to array
• (object) - cast to object
Note that tabs and spaces are allowed inside the parentheses, so the following are functionally
equivalent:
$foo = (int) $bar;
$foo = ( int ) $bar;
85 |