How to determine whether an array value is empty in php

PHPz
Release: 2023-04-26 14:40:47
Original
893 people have browsed it

In PHP, we can use some built-in functions and operators to detect whether the value of an array is empty.

  1. isset() function

isset() function can be used to check whether a variable has been set and is not null. If the key value of an array exists and the value is not null, isset() will return true, otherwise it will return false. Therefore, it can also be used to determine whether an array element is empty. For example:

$arr = array('foo' => null, 'bar' => 'value');
if(isset($arr['foo'])){
    echo "foo is set";
} else {
    echo "foo is not set";
}

if(isset($arr['bar'])){
    echo "bar is set";
} else {
    echo "bar is not set";
}
Copy after login

The output result is:

foo is set
bar is set
Copy after login
  1. empty() function

empty() function can be used to detect whether a value is empty. It will return a boolean value. empty() will return true if a variable is 0, an empty string, null, false, an empty array, or an object with no properties. empty() also returns true if an array key exists but the value is empty. For example:

$arr = array('foo' => '', 'bar' => 'value');
if(empty($arr['foo'])){
    echo "foo is empty";
} else {
    echo "foo is not empty";
}

if(empty($arr['bar'])){
    echo "bar is empty";
} else {
    echo "bar is not empty";
}
Copy after login

The output result is:

foo is empty
bar is not empty
Copy after login

It should be noted that empty() can only be used for variables, not constants or expressions.

  1. is_null() function

The is_null() function can be used to check whether a variable is null. If it is null, it returns true, otherwise it returns false. Therefore, it can be used to determine whether an array element is null. For example:

$arr = array('foo' => null, 'bar' => 'value');
if(is_null($arr['foo'])){
    echo "foo is null";
} else {
    echo "foo is not null";
}

if(is_null($arr['bar'])){
    echo "bar is null";
} else {
    echo "bar is not null";
}
Copy after login

The output result is:

foo is null
bar is not null
Copy after login
  1. array_key_exists() function

array_key_exists() function can be used to check whether an array contains the specified The key name, returns true if it exists, otherwise returns false. Therefore, it can also be used to determine whether an array element exists. For example:

$arr = array('foo' => null, 'bar' => 'value');
if(array_key_exists('foo', $arr)){
    echo "foo exists";
} else {
    echo "foo does not exist";
}

if(array_key_exists('bar', $arr)){
    echo "bar exists";
} else {
    echo "bar does not exist";
}
Copy after login

The output result is:

foo exists
bar exists
Copy after login

To sum up, the above four methods can be used to determine whether the array element is empty. Different methods are suitable for different scenarios, and you need to choose according to the actual situation.

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