有人能解释一下为什么这是非空返回吗?
<?php
$attributes=array("description"=>"","quantity"=>"","price"=>"","discount"=>"");
if(empty($attributes))
echo 'empty';
else
echo 'non empty';
exit;
?>发布于 2014-03-29 07:27:18
来自empty()
确定变量是否为空。如果变量不存在,或者它的值等于FALSE,则该变量被认为是空的。如果变量不存在,则空()不会生成警告。
在这种情况下,变量$attributes存在,并且它不等于FALSE。因此,empty()将返回布尔FALSE。
来检查它们是否都是空的,并回显一条消息:
foreach ($attributes as $key => $value) {
if (empty($value)) {
echo "'$key' is empty\n";
}
}检查 all 数组值是否为空:
if(!array_filter($attributes)) {
echo 'All values are empty';
}以检查数组值的任何是否为空:
if (array_search('', $attributes) !== FALSE) {
echo 'One of the values in the array is empty';
}发布于 2014-03-29 07:29:14
这是因为您有一个空字符串数组,而不是一个空数组。
https://stackoverflow.com/questions/22728265
复制相似问题