How to determine whether a php array is empty (three methods)

PHPz
Release: 2023-04-12 13:40:14
Original
3019 people have browsed it

In PHP, there are several ways to determine whether an array is empty. In this article, we will discuss how to use these methods to check whether an array is empty.

Method 1: Use the empty function

The empty() function is a built-in function of PHP that can be used to determine whether a variable is empty. When applied to an array, the empty() function will return true or false, depending on whether the array is empty.

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

$array1 = array();
$array2 = array("apple", "banana");

if (empty($array1)) {
    echo "数组1为空";
} else {
    echo "数组1不为空";
}

if (empty($array2)) {
    echo "数组2为空";
} else {
    echo "数组2不为空";
}
Copy after login

The output result is:

数组1为空
数组2不为空
Copy after login
Copy after login
Copy after login

In the above code, we first define Two arrays: $array1 and $array2. Then, we use the empty() function to check if both arrays are empty. Because $array1 is empty, the first if statement outputs "array1 is empty". And because $array2 is not empty, the second if statement outputs "array2 is not empty".

Method 2: Use the count function

Another commonly used function in PHP is the count() function. This function can be used to count the number of elements in an array. When applied to an empty array, the count() function will return 0, so it can be used to determine whether the array is empty.

The following is a sample code that uses the count() function to determine whether the array is empty:

$array1 = array();
$array2 = array("apple", "banana");

if (count($array1) == 0) {
    echo "数组1为空";
} else {
    echo "数组1不为空";
}

if (count($array2) == 0) {
    echo "数组2为空";
} else {
    echo "数组2不为空";
}
Copy after login

The output result is:

数组1为空
数组2不为空
Copy after login
Copy after login
Copy after login

In the above code, we use count( ) function counts the number of elements in two arrays and compares them to 0. If the result is 0, then the array is empty. Therefore, the first if statement outputs "array 1 is empty" and the second if statement outputs "array 2 is not empty".

Method 3: Use the isset function

The last method to determine whether the array is empty is the isset() function. The isset() function is used to check whether a variable has been set and is not empty. When applied to an array, the isset() function will return true or false, depending on whether the array is empty.

The following is a sample code that uses isset() function to determine whether the array is empty:

$array1 = array();
$array2 = array("apple", "banana");

if (isset($array1) && empty($array1)) {
    echo "数组1为空";
} else {
    echo "数组1不为空";
}

if (isset($array2) && empty($array2)) {
    echo "数组2为空";
} else {
    echo "数组2不为空";
}
Copy after login

The output result is:

数组1为空
数组2不为空
Copy after login
Copy after login
Copy after login

In the above code, we first use isset () function checks if both arrays are set and checks if they are empty. Because $array1 is empty, the first if statement outputs "array1 is empty". And $array2 is not empty, so the second if statement outputs "array2 is not empty".

Summary

In PHP, we can use the empty() function, count() function or isset() function to check whether an array is empty. No matter which method you choose, you can determine whether an array is empty and respond appropriately in your code.

The above is the detailed content of How to determine whether a php array is empty (three methods). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template