Home >Backend Development >PHP Problem >What is the function in PHP to determine if an array is not empty?
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.
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 { //不为空 }
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 { //不为空 }
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(',',$arr); if(empty($str)) echo "空"; else echo "非空";
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!