Home > Article > Backend Development > Teach you how to use PHP functions to detect the type of variables
Variables are the core of PHP. Before we operate a variable, we need to first figure out what type the variable belongs to. So how to detect variable type in PHP? The following article will introduce to you several functions of PHP to detect variable types.
PHP provides many functions for detecting data types, but they can be roughly divided into two types: gettype() function and is_*
class function. Let's take a look at it through a code example.
Let’s take a look at the following example first:
<?php echo gettype(102) ."<br>"; echo gettype(true) ."<br>"; echo gettype(' ') ."<br>"; echo gettype(null) ."<br>"; echo gettype(array()) ."<br>"; echo gettype(new stdclass()); ?>
Look at the output:
The gettype() function can obtain and return The type of variable, the return value can be:
boolean
##integer
double (Since PHP 4, if it is a float, it will return "double" instead of "float")
string
array
(resource) resource is a special variable type that saves a reference to an external resource; it mainly describes a PHP extension resource. Resources are created and used through specialized functions.
NULL is also a special data type. It has only one value, NULL, which means a null value (i.e. The variable has no value).
series of functions to detect the type of variables. Let’s take a look at the following example:
<?php header("Content-type:text/html;charset=utf-8"); $num1 = 123456; $arr = array(1, 2, 3, 4, 5, 6, 7); $bool = false; $str1 = NULL; $str2 = '654321'; $float = 3.1415926; $str3 = 'hello!'; class foo { function dosomething() { echo "你好!"; } } $obj = new foo(); if (is_array($arr)) echo '$arr 是数组'; echo '<br>'; if (!is_array($num1)) echo '$num1 不是数组'; echo '<br>'; if (is_numeric($str2)) echo '$str2 是数字'; echo '<br>'; if (is_bool($bool)) echo '$bool 是数字'; echo '<br>'; if (is_float($float)) echo '$float 是浮点类型'; echo '<br>'; if (!is_float($str2)) echo '$str2 不是浮点类型'; echo '<br>'; if (is_int($num1)) echo '$num1 是整数类型'; echo '<br>'; if (!is_int($float)) echo '$float 不是整数类型'; echo '<br>'; if (is_string($str2)) echo '$str2 是字符串'; echo '<br>'; if (is_object($obj)) echo '$obj 是一个对象'; echo '<br>'; if (!isset($str1)) echo '$str1 未定义或则值为 NULL'; ?>
Look at the output:
is_* Multiple functions with different functions in the series Functions that can be used to check the data type of variables individually. Let’s take a look at several commonly used checking functions:
1. is_int(): Check whether the variable
$var is an integer type (integer), if $var
If yes, TRUE is returned, otherwise FALSE is returned; the syntax is "is_int($var)
", and the alias is is_integer(). 2. is_float(): Check whether the variable
is a floating point type. If $var
is, return TRUE, otherwise return FALSE; syntax "is_float($var)
", aliased as is_real(). 3. is_string(): Check whether the variable
is of string type. If $var
is, return TRUE, otherwise return FALSE; syntax "is_string($var)
". 4. is_array(): Check whether the variable
is an array. If $var
is, return TRUE, otherwise return FALSE; syntax "is_array ($var)
". 5. is_object(): Check whether the variable
is an object. If $var
is, return TRUE, otherwise return FALSE; syntax " is_object($var)
". 6, is_numeric(): Check whether the variable
is a number or a numeric string. If $var
is, return TRUE, otherwise return FALSE; syntax " is_numeric($var)
”. Okay, that’s all. If you want to know anything else, you can click this. → →
Finally, I would like to recommend a free video tutorial on PHP arrays:
PHP function array array function video explanationThe above is the detailed content of Teach you how to use PHP functions to detect the type of variables. For more information, please follow other related articles on the PHP Chinese website!