Home > Backend Development > PHP Problem > How to determine if an array is empty in php

How to determine if an array is empty in php

PHPz
Release: 2023-04-23 19:43:14
Original
552 people have browsed it

In PHP, when defining an array, sometimes it is necessary to check or determine whether the array is empty. You can easily determine whether an array is empty using PHP's own functions.

Method 1: Use the count() function

Use the count() function to get the number of array elements. If the array has no elements, it returns 0, otherwise it returns the number of elements. Therefore, using the count() function to determine whether an array is empty is a simple and effective method.

Sample code:

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

If the array $arr has no elements, the above code will output "array is empty", otherwise it will output "array is not empty".

Method 2: Use empty() function

PHP’s own empty() function can help us determine whether a variable is empty, including empty arrays. The empty() function returns true when the variable is one of the following values, otherwise it returns false:

  • "" (empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a floating point number)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • null Array

Sample code:

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

Different from the count() function, the empty() function can be used to determine whether the variable is empty and set at the same time, so in some cases In this case, it will be more convenient to use this function to determine whether the array is empty.

Method 3: Use the sizeof() function

The sizeof() function in PHP is similar to the count() function and can both return the number of elements in an array. Of course, both return 0 if the array is empty.

Sample code:

$arr = array();
if (sizeof($arr) == 0) {
    echo "数组为空";
} else {
    echo "数组不为空";
}
Copy after login

In addition to returning the number of array elements, the sizeof() function can also return the number of elements of various data types including all data structures of the array.

Summary

It is not difficult to determine whether an array is empty in PHP. We can use any of these functions, count(), empty(), and sizeof() to achieve this purpose. . It is worth noting that when judging whether an array is empty, please pay special attention to the judgment rules to avoid unexpected results.

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