Home  >  Article  >  Backend Development  >  PHP variable type coercion_PHP tutorial

PHP variable type coercion_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:13989browse

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 "Convert Before: $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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320753.htmlTechArticleIn other words, if a string value is assigned to the variable var, var becomes a string. If you assign an integer value to var, it becomes an integer. Type coercion in PHP...
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