函数
PHP内置了检测数据类型的系列函数,可以对不同类型的数据进行检测,判断其是否属于某个类型,如果符合则返回true,否则返回false。检测数据类型的函数如表3.6所示。
函数 | 检测类型 | 实例 |
---|---|---|
is_bool | 检测变量是否为布尔型 | is_bool($test) |
is_string | 检测变量是否为字符型 | is_string($test) |
is_float | 检测变量是否为浮点型 | is_float($test) |
is_int | 检测变量是否为整型 | is_int($test) |
is_null | 检测变量是否为空 | is_null($test) |
is_array | 检测变量是否为数组 | is_array($test) |
is_object | 检测变量是否为对象 | is_object($test) |
is_numeric | 检测变量是否为数字或由数字组成 | is_numeric($test) |
语法比较简单,一般配合if使用,例如:
<?php
$str="123456789"; //整形
if(is_numeric($str)) //判断是否由数字组成
echo "这是由数字组成的字符串</p>";
else
echo "这不是由数字组成的字符串</p>";
$str1="abcd1233";
if(is_numeric($str1)) //判断是否由数字组成
echo "这是由数字组成的字符串</p>";
else
echo "这不是由数字组成的字符串</p>";
?>
运行结果如下: