empty()
The following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
对"空数组"的理解:array() (an empty array),数组元素个数为0.
<?php
$array1=array();
print_r($array1);
if(empty($array1)){
echo '对empty()来说是空数组(an empty array)';
}
else{
echo '对empty()来说是非空数组(an noempty array)';
}
?>
显示结果:######################
Array
(
)
对empty()来说是an empty array
###############################
<?php这个并不是空数组,因为它有一个元素为空字符(""),要注意和空字符("" (an empty string))区别;
$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)
###############################