Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it right to use empty() function for checking an object is null?

Tags:

php

Say,

$obj = $this->someFunc(); // this returns an object

if(empty($obj)){ 
    // suppose $obj is null, it does works correctly
}

In http://php.net/manual/en/function.empty.php, empty() is only used for variables and arrays.

But, is it the correct way?

like image 770
sk8terboi87 ツ Avatar asked Dec 07 '22 09:12

sk8terboi87 ツ


2 Answers

php has the function is_null() to determine whether an object is null or not: http://php.net/manual/en/function.is-null.php

like image 188
ceth Avatar answered May 25 '23 10:05

ceth


null will cause empty() to return true. However, if you're checking to see if that value is actually null, is_null() is better suited for the job.

like image 39
John Conde Avatar answered May 25 '23 10:05

John Conde