php怎么将字符串强转为int类型
本教程操作环境:windows7系统、php7.1版、DELL G3电脑
方法2:使用intval()函数
intval() 函数用于获取变量的整数值。
<?php header("Content-type:text/html;charset=utf-8"); $str = '32.14abc'; $int = intval($str); echo $int."<br>"; echo '变量 $int 的类型为:' . gettype($int) . '<br>'; ?>
说明:intval() 函数通过使用指定的进制 base 转换(默认是十进制),返回变量 var 的 integer 数值。 intval() 不能用于 object,否则会产生 E_NOTICE 错误并返回 1。
方法3:使用settype()函数
settype ( $var ,$type )
函数可将变量$var
设置为指定类型$type
,$type
可设置的值:
-
"boolean" (或为"bool",从 PHP 4.2.0 起)
-
"integer" (或为"int",从 PHP 4.2.0 起)
-
"float" (只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的"double"现已停用)
-
"string"
-
"array"
-
"object"
-
"null" (从 PHP 4.2.0 起)
示例:
<?php header("Content-type:text/html;charset=utf-8"); $str = '3542abc'; settype($str,"integer"); echo $str."<br>"; echo '修改后的类型为:' . gettype($str) . '<br>'; ?>
说明:
settype() 函数会改变变量本身的类型。
版权声明:
作者:后浪云
链接:https://www.idc.net/help/9024/
文章版权归作者所有,未经允许请勿转载。
THE END