php 中的测试对象为空

Maj*_*leb 0 php arrays string null

我想测试一个对象是否为空,我有以下代码

$listcontact = array();
$contact=$ms->search('email','test@live.Fr');
var_dump(($contact));
Run Code Online (Sandbox Code Playgroud)

如果结果$listcontact不为空,则结果如下

object(stdClass)[6]
public 'item' => string 'dfdfsd' (length=7)
Run Code Online (Sandbox Code Playgroud)

如果它是 null ,我得到以下结果

object(stdClass)[6]
Run Code Online (Sandbox Code Playgroud)

如何测试变量$listcontact是否存在?我尝试过is_nulland(empty()但不起作用

Pra*_*man 6

您可以使用内置函数is_null()来检查空值。所以,使用:

if (is_null($listcontact))
  // Yes, it is null.
else
  // Do something.
Run Code Online (Sandbox Code Playgroud)


Ski*_*ᴉʞS 5

使用该函数is_null()如下:

is_null($listcontact);
Run Code Online (Sandbox Code Playgroud)

返回值为

如果 var 为 null,则返回 TRUE,否则返回 FALSE。

编辑

你也可以使用这个:

  if ( !$YOUR_OBJECT->count() ){
        //null
  }
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅这些答案

尝试使用array_filter()

$EmptyArray= array_filter($listcontact);

if (!empty($EmptyArray)){

}
else{
    //nothing there
}
Run Code Online (Sandbox Code Playgroud)