Home >Backend Development >PHP Problem >How to determine whether the value in an array is empty in php

How to determine whether the value in an array is empty in php

PHPz
PHPzOriginal
2023-04-18 17:03:06924browse

In PHP code, we often need to determine whether the value in the array is empty to perform subsequent operations. This article will introduce in detail the method of determining whether an array value is empty in PHP.

1. Determine whether a single array value is empty

To determine whether a single array value is empty, we can use PHP's built-in functions empty() and isset(). The usage of the two functions is as follows: :

  1. empty() function

Syntax: bool empty(mixed $var)

Description: Determine whether the incoming parameter is empty. When the incoming parameter is a specific value such as 0, '', null, false, array(), "", etc., true is returned, otherwise false is returned.

Sample code:

$my_array = array('name' => 'Tom', 'age' => 20, 'gender' => '');
if (empty($my_array['name'])) {
    echo 'name is empty';
} else {
    echo 'name is not empty';
}

Output result: name is not empty

Description: Determine whether the value with the key name name in the $my_array array is empty, because the value of name For Tom, it does not belong to the specific value of the empty() function, so false is returned.

  1. isset() function

Syntax: bool isset(mixed $var[, mixed $...])

Description: Judge the incoming Whether the parameters have been set or exist. When the passed parameter has been set or exists, it returns true, otherwise it returns false.

Sample code:

$my_array = array('name' => 'Tom', 'age' => 20, 'gender' => '');
if (isset($my_array['gender'])) {
    echo 'gender is set';
} else {
    echo 'gender is not set';
}

Output result: gender is set

Description: Determine whether the value with the key name gender in the $my_array array is set. Because the value of gender is an empty string and does not belong to the specific value of the empty() function, true is returned.

2. Determine whether multiple array values ​​are empty

When we need to determine whether multiple array values ​​are empty, we can use a foreach loop to traverse the array to determine. The sample code is as follows:

$my_array = array('name' => '', 'age' => 20, 'gender' => 'Male');
foreach($my_array as $key => $value) {
    if(empty($value)) {
        echo $key.' is empty';
        echo '<br />';
    }
}

Output result: name is empty

Description: Traverse the $my_array array. If the value in the array is empty, the output key name and the value are empty.

3. Summary

This article introduces the method of determining whether an array value is empty in PHP, including determining whether a single array value is empty and determining whether multiple array values ​​are empty. In actual development, we need to choose appropriate methods for judgment based on specific scenarios to avoid unnecessary errors.

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