当前位置: 首页> 函数类别大全> gettype

gettype

获取变量的类型
名称:gettype
分类:变量处理
所属语言:php
一句话介绍:返回变量的类型。

定义和用法

gettype() 函数返回变量的类型。

实例

返回不同变量的类型:

<?php
$a = 3;
echo gettype($a) . "<br>";

$b = 3.2;
echo gettype($b) . "<br>";

$c = "Hello";
echo gettype($c) . "<br>";

$d = array();
echo gettype($d) . "<br>";

$e = array("red", "green", "blue");
echo gettype($e) . "<br>";

$f = NULL;
echo gettype($f) . "<br>";

$g = false;
echo gettype($g) . "<br>";
?>

亲自试一试

例子解释:

gettype() 函数用于检查不同类型的变量(整数、浮点数、字符串、数组、空值和布尔值)并返回它们的类型。例如,对于整数变量 $agettype($a) 将返回字符串 "integer"。对于浮点数变量 $b,它将返回 "double",对于字符串变量 $c,它将返回 "string",依此类推。如果变量是关闭的资源,在 PHP 7.2 及更高版本中,gettype() 将返回 "resource (closed)",而在早期版本中,它可能返回 "unknown type"

语法

gettype(variable);
参数 描述
variable 必需。指定要检查的变量。
同类函数
  • 检测变量是否是数组  is_array

    is_array

    检测变量是否是数组
  • 检测变量是否为 null  is_null

    is_null

    检测变量是否为null
  • 检测变量是否已设置并且非 null  isset

    isset

    检测变量是否已设置并且非null
  • 输出或返回一个变量的字符串表示  var_export

    var_export

    输出或返回一个变量的字符串表示
  • 检测变量是否是布尔型  is_bool

    is_bool

    检测变量是否是布尔型
  • 检查一个变量是否为空  empty

    empty

    检查一个变量是否为空
  • 获取变量的类型  gettype

    gettype

    获取变量的类型
  • 检测变量是否是字符串  is_string

    is_string

    检测变量是否是字符串
热门文章