Notice: Trying to get of non object problem of PHP solution

  • 2020-05-16 06:29:50
  • OfStack

I actually called a database access method of zend, using the fetchAll method. However, since there is no such record in the database, the object returned is null, so I judge whether the object is null:

 
if($obj==null){ 
... 
} 

And as a result of that, you get notice, which is weird, null, and you can't access it, right?

After searching the data, it is found that to judge whether it is null, it is necessary to judge as follows:

 
if (isset($obj)) { 
echo "This var is set set so I will print."; 
} 

What does this isset do?

The isset function detects whether a variable is set.

Format: bool isset (mixed var [, mixed var [,...]])

The return value:

Returns FALSE if the variable does not exist
If the variable exists and its value is NULL, FALSE is also returned
If the variable exists and the value is not NULL, TURE is returned
When multiple variables are checked at the same time, TRUE is returned when each item meets the previous requirement; otherwise, FALSE is the result
Once a variable has been freed using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, FALSE is returned. Note also that 1 NULL byte (" \0 ") is not the same as PHP's NULL constant.

Warning: isset() can only be used with variables, because passing any other parameter will cause a parsing error. To check if the constant is set, use the defined() function.

It seems that the problem with my judgment just now is that "is 1 NULL byte (" \0") is not the same as NULL constant of PHP ".

Related articles: