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"
範例
示例 #1 gettype() 示例
<?php
$data = array(1, 1., NULL, new stdClass, 'foo');
foreach ($data as $value) {
echo gettype($value), "\n";
}
?>
以上例程的輸出類似於:
integer double NULL object string
更新日誌
版本 | 說明 |
---|---|
7.2.0 |
現在,已關閉的資源報告為 'resource (closed)' 。
此前,已關閉的資源報告為 'unknown 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() - 檢查類的方法是否存在