gettype
(PHP 4, PHP 5, PHP 7, PHP 8)
gettype — 获取变量的类型
参数
value
-
要检查类型的变量。
返回值
返回字符串,可能值为:
-
"boolean"
-
"integer"
-
"double"
(由于历史原因,如果是浮点型,则返回"double"
,而不仅仅是"float"
) -
"string"
-
"array"
-
"object"
-
"resource"
-
"resource (closed)"
自 PHP 7.2.0 起 -
"NULL"
-
"unknown type"
更新日志
版本 | 说明 |
---|---|
7.2.0 |
现在,已关闭的资源报告为 'resource (closed)' 。此前,已关闭的资源报告为
'unknown type' 。
|
示例
示例 #1 gettype() 示例
<?php
$data = array(1, 1., NULL, new stdClass, 'foo');
foreach ($data as $value) {
echo gettype($value), "\n";
}
?>
以上示例的输出类似于:
integer double NULL object string
参见
- get_debug_type() - 以适合调试的方式获取变量的类型名称
- settype() - 设置变量的类型
- get_class() - 返回对象的类名
- is_array() - 检测变量是否是数组
- is_bool() - 检测变量是否是布尔值
- is_callable() - 验证值是否可以在当前范围内作为函数调用
- is_float() - 检测变量是否是浮点型
- is_int() - 检测变量是否是整数
- is_null() - 检测变量是否是 null
- is_numeric() - 检测变量是否是数字或数字字符串
- is_object() - 检测变量是否是对象
- is_resource() - 查找变量是否为资源
- is_scalar() - 查找变量是否是标量
- is_string() - 检测变量的类型是否是字符串
- function_exists() - 如果给定的函数已经被定义就返回 true
- method_exists() - 检查类的方法是否存在
+添加备注
用户贡献的备注 1 note
mohammad dot alavi1990 at gmail dot com ¶
1 year ago
Be careful comparing ReflectionParameter::getType() and gettype() as they will not return the same results for a given type.
string - string // OK
int - integer // Type mismatch
bool - boolean // Type mismatch
array - array // OK