Home >Backend Development >PHP Problem >What is the function in php to detect whether the array is all empty?
Two functions to detect whether the array is all empty: 1. empty(), just pass the array variable into this function, the syntax is "empty($arr)", if it returns true, all is empty, otherwise it is not empty. 2. count() can obtain the array length. If the obtained array length is less than 1, the array is empty, otherwise it is not empty.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use some two functions To detect whether the array is all empty:
empty() function
count() function
1. Use the empty() function
The empty() function is used to check whether a variable is empty.
empty() Determines whether a variable is considered empty. When a variable does not exist, or its value is equal to FALSE, then it is considered not to exist. empty() does not generate a warning if the variable does not exist.
Pass the array into this function. If it is true, it means it is empty;
<?php header("Content-type:text/html;charset=utf-8"); $arr = []; var_dump($arr); if (empty($arr)) { echo "数组为空<br>"; } else { echo "数组不为空<br>"; } ?>
2. Use the count() function
The count() function can count the number of all elements in the array, that is, obtain the length of the array.
If the length of the array obtained is 0, or less than 1, it means that there are no elements in the array, that is, it is an empty array.
<?php header("Content-type:text/html;charset=utf-8"); $arr = [1,2]; var_dump($arr); if (count($arr) < 1) { echo "数组为空<br>"; } else { echo "数组不为空<br>"; } ?>
Recommended learning: "PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of What is the function in php to detect whether the array is all empty?. For more information, please follow other related articles on the PHP Chinese website!