我想测试对象是否为null,我有以下代码
$listcontact = array();
$contact=$ms->search('email','test@live.Fr');
var_dump(($contact));
,如果$listcontact
不为空,则结果如下:
object(stdClass)[6]
public 'item' => string 'dfdfsd' (length=7)
如果它为空,我得到以下结果
object(stdClass)[6]
我如何测试变量$listcontact
是否存在?我已尝试使用is_null
和(empty()
但未使用
答案 0 :(得分:3)
您可以使用内置函数is_null()
来检查空值。所以,使用:
if (is_null($listcontact))
// Yes, it is null.
else
// Do something.
答案 1 :(得分:2)
使用函数is_null()
,如下所示:
is_null($listcontact);
返回值是:
如果var为null,则返回TRUE,否则返回FALSE。
你也可以使用它:
if ( !$YOUR_OBJECT->count() ){
//null
}
有关详细信息,请参阅answers
尝试使用array_filter()
$EmptyArray= array_filter($listcontact);
if (!empty($EmptyArray)){
}
else{
//nothing there
}
答案 2 :(得分:2)
如果使用var_dump获得object stdclass
,则不能为空。
检查变量是否为空的最快方法是使用$listcontact === null
。
如果它为null,我得到以下结果
对象(stdClass的)[6]
这意味着search()
函数没有返回null。
答案 3 :(得分:0)
我找到了正确答案
$tmp = (array) $listcontact;
var_dump(empty($tmp));
if(empty($tmp)){
echo "empty"
}