In PHP, there are many ways to determine whether a variable is an array, which are introduced one by one below.
Method 1: is_array function
The is_array function is the most commonly used method in PHP to determine whether a variable is an array. The usage of the is_array function is as follows:
$is_array = is_array($variable);
Among them, $variable represents the variable to be judged.
The return value of the is_array function is of boolean type (true or false). If $variable is an array, it returns true, otherwise it returns false.
The following is an example:
$data = ['apple', 'banana', 'pear']; $is_array = is_array($data); //true
Method 2: gettype function
The gettype function can return the type of the variable. The usage method is as follows:
$type = gettype($variable);
Among them, $variable represents the variable to be judged.
If the value of $type is "array", it means that $variable is an array type, otherwise it is not an array type.
The following is an example:
$data = ['apple', 'banana', 'pear']; $type = gettype($data); //"array"
Method 3: Use built-in functions
In addition to the methods mentioned above, PHP also provides some built-in functions to determine variable types.
For example, if you want to check whether a variable is an array, you can use the following function:
The is_array() function can determine whether a variable is an array type. The usage method is the same as described above:
$is_array = is_array($variable);
The usage method of other functions is similar to this.
The following is an example:
$data = ['apple', 'banana', 'pear']; $is_array = is_array($data); //true
Summary
In PHP, there are many ways to determine whether a variable is an array, the most commonly used method is to use the is_array function, others Methods include using the gettype function and some built-in functions. No matter which method is used, you can easily determine whether a variable is an array type, and you can choose the appropriate method according to your needs.
The above is the detailed content of How to determine whether it is an array in php. For more information, please follow other related articles on the PHP Chinese website!