" //Output: $foo"/> " //Output: $foo">

Home  >  Article  >  Backend Development  >  Variable type PHP variable type coercion

Variable type PHP variable type coercion

WBOY
WBOYOriginal
2016-07-29 08:41:031241browse

That is, if you assign a string value to the variable var, var becomes a string. If you assign an integer value to var, it becomes an integer.
Type casting in PHP is very much like in C: the variable to be converted is preceded by the target type enclosed in parentheses.

Copy code The code is as follows:


$foo = 10;
echo "Before conversion: $foo=".$foo; //Output an integer
echo "
" //Output: $foo=10
echo "
";
$foo = (boolean) $foo; //Forced to Boolean
echo "After conversion: $foo=". $foo; //Output: $foo=1
?>


The allowed casts are:
(int), (integer) - converted to integer type
(bool), (boolean) - converted to Boolean type
(float), (double), (real) - Convert to floating point type
(string) - Convert to string
(array) - Convert to array
(object) - Convert to object

The above has introduced the forced conversion of variable types in PHP, including the content of variable types. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn