Home  >  Article  >  Backend Development  >  Teach you how to use PHP functions to detect the type of variables

Teach you how to use PHP functions to detect the type of variables

青灯夜游
青灯夜游Original
2021-08-05 19:09:255611browse

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:

";
echo gettype(true) ."
"; echo gettype(' ') ."
"; echo gettype(null) ."
"; echo gettype(array()) ."
"; echo gettype(new stdclass()); ?>

Look at the output:

Teach you how to use PHP functions to detect the type of variables

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

  • ##object

  • resource

    (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

    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).


  • unknown type

  • gettype() function contains comparison of strings, so the operation is slower. In addition to this function, we can also use the
is_*

series of functions to detect the type of variables. Let’s take a look at the following example:

';
if (!is_array($num1))
	echo '$num1 不是数组';
echo '
'; if (is_numeric($str2)) echo '$str2 是数字'; echo '
'; if (is_bool($bool)) echo '$bool 是数字'; echo '
'; if (is_float($float)) echo '$float 是浮点类型'; echo '
'; if (!is_float($str2)) echo '$str2 不是浮点类型'; echo '
'; if (is_int($num1)) echo '$num1 是整数类型'; echo '
'; if (!is_int($float)) echo '$float 不是整数类型'; echo '
'; if (is_string($str2)) echo '$str2 是字符串'; echo '
'; if (is_object($obj)) echo '$obj 是一个对象'; echo '
'; if (!isset($str1)) echo '$str1 未定义或则值为 NULL'; ?>

Look at the output:

Teach you how to use PHP functions to detect the type of variablesis_* 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 $varIf 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

$var

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

$var

is of string type. If $var is, return TRUE, otherwise return FALSE; syntax "is_string($var)". 4. is_array(): Check whether the variable

$var

is an array. If $var is, return TRUE, otherwise return FALSE; syntax "is_array ($var)". 5. is_object(): Check whether the variable

$var

is an object. If $var is, return TRUE, otherwise return FALSE; syntax " is_object($var)". 6, is_numeric(): Check whether the variable

$var

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. → →

php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:

PHP function array array function video explanation

, come and learn!

The 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!

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