php 中可以通过 gettype() 函数获取变量类型,该函数返回字符串形式的类型信息,如 string、integer 等。另外,is_* 函数系列可用于判断特定类型,如 is_string() 判断是否是字符串。
如何在 PHP 函数中判断变量类型?
在 PHP 中,确定变量类型的最便捷方式是使用 gettype()
函数。此函数返回一个字符串,表示变量的类型。以下是一些示例:
$variable = \'string\'; echo gettype($variable); // 输出:string $variable = 123; echo gettype($variable); // 输出:integer $variable = 123.45; echo gettype($variable); // 输出:double $variable = true; echo gettype($variable); // 输出:boolean $variable = []; echo gettype($variable); // 输出:array $variable = new stdClass(); echo gettype($variable); // 输出:object