PHP - gettype() 函式



定義和用法

gettype() 函式返回變數的型別。

語法

string gettype ( mixed $value )

引數

序號 引數 & 描述
1

將要檢查其型別的變數。

*mixed: mixed 表示引數可以接受多種(但不一定是所有)型別

返回值

此函式返回一個字串。返回字串的可能值如下:

  • boolean

  • integer

  • double(出於歷史原因,double 用於表示float,而不是簡單的 float)

  • string

  • array

  • object

  • resource

  • resource (closed) (自 PHP 7.2.0 起)

  • NULL

  • unknown type

依賴

PHP 4.0 及以上版本

示例

以下示例演示了對不同型別變數使用 gettype():

<?php
   $data = array(2, 2.0, NULL, new stdClass, 'tutPoint', true, array(1, 2, 3), tmpfile());

   foreach ($data as $value) {
      echo gettype($value);
   }
?>

輸出

這將產生以下結果:

integer
double
NULL
object
string
boolean
array
resource
php_variable_handling_functions.htm
廣告