Home > php教程 > php手册 > body text

PHP 变量类型的强制转换

WBOY
Release: 2016-06-13 12:21:01
Original
1188 people have browsed it

也就是说,如果把一个字符串值赋给变量 var,var 就成了一个字符串。如果又把一个整型值赋给 var,那它就成了一个整数。
PHP 中的类型强制转换和 C 中的非常像:在要转换的变量之前加上用括号括起来的目标类型。

复制代码 代码如下:


$foo = 10;
echo "转换前:\$foo=".$foo; //输出一个整数
echo "
" //输出:$foo=10
echo "
";
$foo = (boolean) $foo; //强制转换为布尔型
echo "转换后:\$foo=".$foo; //输出:$foo=1
?>


允许的强制转换有:
(int),(integer) - 转换成整型
(bool),(boolean) - 转换成布尔型
(float),(double),(real) - 转换成浮点型
(string) - 转换成字符串
(array) - 转换成数组
(object) - 转换成对象
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template