Home >Backend Development >PHP Tutorial >php empty() function in detail_PHP tutorial
Today we are going to talk about the usage of the empty function, what is the difference between it and waiting for empty, and whether it returns a normal value when using it to operate an array. Friends in need can refer to it.
代码如下 | 复制代码 |
$array1=array(); print_r($array1); if(empty($array1)){ echo '对empty()来说是空数组(an empty array)'; } else{ echo '对empty()来说是非空数组(an noempty array)'; } ?> 显示结果:###################### Array ( ) 对empty()来说是an empty array |
###############################
代码如下 | 复制代码 |
$array1=array(); $array1[]=''; print_r($array1); if(empty($array1)){ echo '对empty()来说是空数组(an empty array)'; } else{ echo '对empty()来说是非空数组(an noempty array)'; } ?> 显示结果:###################### Array ( [0] => ) 对empty()来说是非空数组(an noempty array) ############################### |
This is not an empty array because one of its elements is an empty character (""). Please pay attention to the difference from an empty character ("" (an empty string));
actually
empty($x) is equal to !isset($x) || !$x
!empty($x) is equal to isset($x) && $x
About empty function syntax
empty -- Check if a variable is empty
Description
bool empty (mixed var)
If var is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any properties will be considered empty, and TRUE is returned if var is empty.