可以使用PHP内置的函数 gettype()
来获取PHP变量的数据类型,例如:
$num = 100;
$str = "hello";
$bool = true;
$null = null;
echo gettype($num); // 输出: integer
echo gettype($str); // 输出: string
echo gettype($bool); // 输出: boolean
echo gettype($null); // 输出: NULL
除了 gettype()
函数外,还可以使用 is_*()
系列函数来检查变量的具体类型,例如:
$num = 100;
if (is_int($num)) {
echo "这是一个整型";
}
常用的 is_*()
函数包括 is_int()
, is_float()
, is_string()
, is_bool()
, is_array()
, is_object()
等。