PHP does not need (or does not support) explicit type definitions in variable definitions; The variable type is determined by the context in which the variable is used.
That is, if you assign a string value to the variable Var,var it becomes a string. If you assign an integer value to Var, it becomes an integer.
The type casts in PHP are very similar to those in C: precede the variables to be converted with the target type enclosed in parentheses.
<? PHP $foo = 10; echo "Before conversion: \ $foo =". $foo ; // echo "<br/>" // output: $foo =10 echo " <br/> "; $foo = (boolean ) $foo ; // cast to Boolean echo "After conversion: \ $foo =". $foo ; // output: $foo =1 ?>
The allowable casts are:
(int), (integer)-Converted to integral type
(bool), (Boolean)-converted to Boolean
(float), (double), (real)-converts to floating-point type
(string)-converts to a string
(array)-Convert an array
(object)-Convert to Object
<? PHP $test = (object) []; // PHP creates an empty object $test->age = ' + '; $test->sex = ' 1 '; Print_r ($test// output Stdclass Object ([age] [sex] = 1)?>
<? PHP $test New StdClass (); // PHP creates an empty object $test->age = ' + '; $test->sex = ' 1 '; Print_r ($test// output Stdclass Object ([age] [sex] = 1)?>
PHP Variable type cast & Create empty Object