Home  >  Article  >  Backend Development  >  How to determine whether a variable is an array in php

How to determine whether a variable is an array in php

PHPz
PHPzOriginal
2023-04-19 09:20:13629browse

PHP is a very popular programming language, and almost all web development uses PHP. In PHP, determining whether a variable is an array is a very common operation, because arrays are one of the most commonly used data types in PHP. This article will introduce how to determine whether a variable is an array in PHP.

1. Use the is_array() function

PHP provides a function is_array() specifically used to determine whether a variable is an array. This function is very simple. You only need to enter the A variable is passed to it as a parameter. If the variable is an array, the function will return true, otherwise it will return false.

The following is a sample code to determine whether a variable is an array:

<?php
    $array = array(1, 2, 3);   // 定义一个数组
    $not_array = &#39;Hello&#39;;      // 定义一个不是数组的变量

    if (is_array($array)) {
        echo &#39;$array是一个数组。&#39;;
    } else {
        echo &#39;$array不是一个数组。&#39;;
    }

    if (is_array($not_array)) {
        echo &#39;$not_array是一个数组。&#39;;
    } else {
        echo &#39;$not_array不是一个数组。&#39;;
    }
?>

Running the above code will output:

$array是一个数组。
$not_array不是一个数组。

2. Use the gettype() function

Another way to determine whether a variable is an array is to use the gettype() function. This function can return the type of a variable. If the variable is an array, it will return "array".

The following is a sample code that uses the gettype() function to determine whether a variable is an array:

<?php
    $array = array(1, 2, 3);   // 定义一个数组
    $not_array = &#39;Hello&#39;;      // 定义一个不是数组的变量

    if (gettype($array) == &#39;array&#39;) {
        echo &#39;$array是一个数组。&#39;;
    } else {
        echo &#39;$array不是一个数组。&#39;;
    }

    if (gettype($not_array) == &#39;array&#39;) {
        echo &#39;$not_array是一个数组。&#39;;
    } else {
        echo &#39;$not_array不是一个数组。&#39;;
    }
?>

Running the above code will output:

$array是一个数组。
$not_array不是一个数组。

3. Use is_int( )Function

In addition to the is_array() and gettype() functions, there is also a function that can determine whether a variable is an array, that is the is_int() function. Although the function name does not contain "array", it can also be used to determine whether the variable is an array. It is different from is_array() and gettype(). The is_int() function is used to determine whether a variable is an integer. However, in PHP, integer type variables cannot be converted into arrays. Therefore, if you use the is_int() function to determine If a variable is true, it means that the variable is definitely not an array.

The following is a sample code that uses the is_int() function to determine whether a variable is an array:

<?php
    $array = array(1, 2, 3);   // 定义一个数组
    $not_array = &#39;Hello&#39;;      // 定义一个不是数组的变量

    if (is_int($array)) {
        echo &#39;$array是一个整数,肯定不是一个数组。&#39;;
    } else {
        echo &#39;$array不是一个整数,可能是一个数组。&#39;;
    }

    if (is_int($not_array)) {
        echo &#39;$not_array是一个整数,肯定不是一个数组。&#39;;
    } else {
        echo &#39;$not_array不是一个整数,可能是一个数组。&#39;;
    }
?>

Running the above code will output:

$array不是一个整数,可能是一个数组。
$not_array不是一个整数,可能是一个数组。

4. Summary

After the above introduction, we can know that there are three ways to determine whether a variable is an array in PHP: using the is_array() function, using the gettype() function and using the is_int() function. Among them, the is_array() function is the most commonly used method and the simplest method. The application scenarios of these three methods are different, and which method to use needs to be decided based on the actual situation.

The above is the detailed content of How to determine whether a variable is an array 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