What is the function in PHP to determine if an array is not empty?

青灯夜游
Release: 2023-03-15 16:32:02
Original
2643 people have browsed it

php function to determine if the array is not empty: 1. empty() function, syntax "empty($arr)", if the return value is false, the array is not empty; 2. count() function, Syntax "count($arr)", if the return value is greater than or equal to 1, the array is not empty.

What is the function in PHP to determine if an array is not empty?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php determines whether the array is Empty function

1, empty() function

Use the function "empty()" function to judge, and pass the array into this function. If it is true, it means it is empty; if it is false, it means it is not empty.

$arr = [];
if (empty($arr)) {
 //为空
} else {
 //不为空
}
Copy after login

2. Count() function

Use the "count()" function to obtain the number of array items, and then determine whether it is less than 1 based on the number of items. If it is less than 1 , which means it is empty;

$arr = [];
if (count($arr) < 1) {
 //为空
} else {
 //不为空
}
Copy after login

Extended knowledge:

Use implode() to output the array as a string and determine whether the output string is empty . At first glance, it seems to be a good method, but unfortunately, like the previous point, it does not work for arrays with more than two dimensions. For example:

$arr= array(array(),array(),arr(www.php.cn)ay());
$str = implode(&#39;,&#39;,$arr);
if(empty($str)) echo "空";
else echo "非空";
Copy after login

Obviously $arr is a two-dimensional array containing three empty arrays, which should be considered empty, but the output is indeed non-empty. Judgment failed.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the function in PHP to determine if an array is not empty?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!