Home>Article>Backend Development> How to use gettype() function to determine the type of variable in PHP?

How to use gettype() function to determine the type of variable in PHP?

青灯夜游
青灯夜游 Original
2019-04-13 10:44:14 6292browse

gettype()函数是PHP中的一个内置函数,用于获取变量的类型;它可用于检查现有变量的类型。下面本篇文章就来给大家介绍一下gettype()函数,希望对大家有所帮助。【视频教程推荐:PHP教程

How to use gettype() function to determine the type of variable in PHP?

基本语法

string gettype ( $var )

参数:gettype()函数接受单个参数$va,表示需要检查其类型的变量的名称。

返回值:此函数会返回字符串类型的值。返回字符串的可能值为:

 ● boolean:表示变量为布尔类型

 ● integer:表示变量为整数类型

 ● double :表示变量为float类型(由于历史原因,如果为float类型,则返回“double”)。

 ● string:表示变量为string类型

 ● array:表示变量为数组类型

 ● object:表示变量为对象类型

 ● resource:表示变量为resource类型

 ● NULL:表示变量为NULL类型

 ● unknown type:未知类型

代码示例

下面我们通过代码示例来看看gettype()函数的使用。

示例1:

"; echo gettype($var2)."
"; echo gettype($var3)."
"; echo gettype($var4)."
"; echo gettype($var5)."
"; echo gettype($var6)."
"; echo gettype($var7)."
"; echo gettype($var8)."
"; ?>

输出:

boolean integer double string array object NULL resource

示例2:

"; echo "变量\$var2=".$var2." ,类型为:".gettype($var2)."
"; echo "变量\$var3=".$var3." ,类型为:".gettype($var3)."
"; echo "变量\$var4=".$var4." ,类型为:".gettype($var4)."
"; echo "变量\$var5=".$var5." ,类型为:".gettype($var5). "
"; echo "变量\$var6=".$var6." ,类型为:".gettype($var6); ?>

输出:

变量$var1=php ,类型为:string 变量$var2=3 ,类型为:integer 变量$var3=100 ,类型为:integer 变量$var4=3.1622776601684 ,类型为:double 变量$var5=NAN ,类型为:double 变量$var6=2 ,类型为:double

说明:pow(x,y) 函数返回 x 的 y 次方,返回值是 Integer或Float类型;sqrt() 函数返回一个数的平方根,返回值为Float类型的。NAN(非数),是计算机科学中数值数据类型的一类值,表示未定义或不可表示的值。

以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。更多精彩内容大家可以关注php中文网相关教程栏目!!!

The above is the detailed content of How to use gettype() function to determine the type of variable in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is the use of php oop? Next article:What is the use of php oop?