Determine whether the value is empty after retrieving it from the database. This seems very simple. Just compare it with null, but it is not the case (recommended learning: PHP Programming from Beginner to master)
if($obj==null){ }
Writing like this will report an error: Notice: Trying to get property of non-object problem,
After checking, I found that I need to use the following writing
if (isset($obj)) { echo "This var is set set so I will print."; }
What does this isset do?
isset function is to detect whether the variable is set.
格式:bool isset ( mixed var [, mixed var [, ...]] )
Return value:
If the variable does not exist, return FALSE
If the variable exists and its value is NULL, also returns FALSE
If the variable exists and the value is not NULL, TRUE is returned
When multiple variables are checked at the same time, each single item is TRUE is returned only when the previous requirement is met, otherwise the result is FALSE
If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("\0") is not equivalent to PHP's NULL constant.
Warning: isset() can only be used with variables, because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.
It seems that the problem with my judgment just now is because this "is a NULL byte ("\0") and is not equivalent to PHP's NULL constant".
The above is the detailed content of php check if variable is empty. For more information, please follow other related articles on the PHP Chinese website!