Home > Backend Development > PHP Problem > How to determine empty array and empty object in PHP

How to determine empty array and empty object in PHP

PHPz
Release: 2023-04-17 14:52:08
Original
1080 people have browsed it

How PHP determines empty arrays and empty objects

In PHP, we often need to determine the type and status of a variable or expression. When dealing with arrays and objects, it is a common need to determine whether they are empty. This article will introduce how to determine empty arrays and empty objects in PHP.

  1. Judge empty array
    In PHP, we can use the empty() function or count() function to determine whether an array is empty.

empty() function
empty() function can check whether a variable is empty. This function will return true when the variable is not declared, the value is false, the value is the empty string, the value is 0, the value is "0", the value is null, or the value is an empty array.

Example:

$arr1 = array();
if (empty($arr1)) {
    echo "数组为空。";
} else {
    echo "数组不为空。";
}
Copy after login

count() function
count() function is used to count the number of array elements. If an array is empty, that is, the number of elements is 0, the count() function returns 0.

Example:

$arr2 = array();
if (count($arr2) == 0) {
    echo "数组为空。";
} else {
    echo "数组不为空。";
}
Copy after login
  1. Determining empty objects
    In PHP, we can use the empty() function or is_null() function to determine whether an object is empty.

empty() function
When the empty() function checks whether the object is empty, it will first call the object's __isset() magic method to determine whether the object's attributes exist. If the attributes do not exist, , then returns true. Then call the object's __get() magic method to get the attribute value. If the attribute value is null, it returns true.

Example:

class Student {
    private $name;
    public function __isset($key) {
        return isset($this->$key);
    }
    public function __get($key) {
        return $this->$key;
    }
}

$stu = new Student();
if (empty($stu->name)) {
    echo "对象属性为空。";
} else {
    echo "对象属性不为空。";
}
Copy after login

is_null() function
is_null() function is used to check whether a variable is null. When checking whether the object is empty, you can check whether the object variable is null. .

Example:

$obj = null;
if (is_null($obj)) {
    echo "对象为空。";
} else {
    echo "对象不为空。";
}
Copy after login

Summary
In PHP, the methods of judging empty arrays and empty objects are different, but they are all based on the empty() function and is_null() function. . Understanding these methods can help us better handle array and object operations, improve coding efficiency and code robustness.

The above is the detailed content of How to determine empty array and empty object 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