PHP 7.4.25 Released!

gettype

(PHP 4, PHP 5, PHP 7, PHP 8)

gettype获取变量的类型

说明

gettype(mixed $value): string

返回 PHP value 变量的类型。 对于类型检查,请使用 is_* 函数。

参数

value

要检查类型的变量。

返回值

返回字符串,可能值为:

  • "boolean"
  • "integer"
  • "double" (由于历史原因,如果是浮点型,则返回 "double",而不仅仅是 "float"
  • "string"
  • "array"
  • "object"
  • "resource"
  • "resource (closed)" 自 PHP 7.2.0 起
  • "NULL"
  • "unknown type"

范例

示例 #1 gettype() 示例

<?php

$data 
= array(11.NULL, new stdClass'foo');

foreach (
$data as $value) {
    echo 
gettype($value), "\n";
}

?>

以上例程的输出类似于:

integer
double
NULL
object
string

更新日志

版本 说明
7.2.0 现在,已关闭的资源报告为 'resource (closed)'。 此前,已关闭的资源报告为 'unknown type'

参见

add a note add a note

User Contributed Notes 1 note

up
-15
matt at appstate
16 years ago
Here is something that had me stumped with regards to gettype and is_object.
Gettype will report an incomplete object as such, whereas is_object will return FALSE.

<?php
if (!is_object($incomplete_obj)) {
   echo
'This variable is not an object, it is a/an ' . gettype($incomplete_obj);
}
?>

Will print:
This variable is not an object, it is a/an object
To Top