$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