$obj = $this->someFunc(); // this returns an object
if(empty($obj)){
// suppose $obj is null, it does works correctly
}
在http://php.net/manual/en/function.empty.php,empty()
仅用于变量和数组。
但是,这是正确的方法吗?
$obj = $this->someFunc(); // this returns an object
if(empty($obj)){
// suppose $obj is null, it does works correctly
}
在http://php.net/manual/en/function.empty.php,empty()
仅用于变量和数组。
但是,这是正确的方法吗?
PHP具有的功能is_null()
确定对象是否为空或不是:http://php.net/manual/en/function.is-null.php
null
将导致empty()
返回true。但是,如果您要检查该值是否实际为空,则is_null()
更适合该工作。
if (is_object($obj)) {
// It is an object
}
对不起,回答要快。只是检查:
if ($obj === null) {
// Object is null
} else {
// Object isn't null
}
这也可以做:
if (is_null($obj)) {
// Object is null
}
使用is_null
检查对象为空或不是。
您可以使用is_null()
这个
的对象没有属性不是空的了,因为PHP 5的我用isset很多仄,它有一个值和该值的心不是NULL
如果返回一个空对象....它会工作吗? – 2013-03-07 12:55:37
No. null!=没有值。在这种情况下'empty()'更好。 – 2013-03-07 13:45:29