There are many ways to determine whether a variable is NULL in PHP:
is_null and isset
These two functions Both can be used to determine whether a variable is NULL. They have the same recognition of empty strings, 0, and false. That is is_null=! isset().
But isset is a grammatical structure and is_null is a function. In terms of performance, the grammatical structure is relatively better. Therefore, many places recommend using isset instead of is_null.
== and ===
In some cases, it is recommended to use isset to determine whether a variable is NULL.
But semantically speaking, "whether a variable has been explicitly initialized" and "whether it is NULL" are different concepts. It is inappropriate to use isset in some scenarios, such as checking the return value of a function. Whether it is NULL. At this time, you can use "==" and "====" to determine whether they are NULL.
As for "==" and "===", their direct difference is still very big.
For "==", it recognizes the empty string, 0, and false are both NULL. For "===", only if a variable is really NULL, it represents NULL.
In addition, the performance of "===" is basically similar to "isset", or even better.
So to sum up the above, the best way to judge whether a variable is NULL is to use "===" directly, so that you don't have to hesitate between is_null and isset. In fact, the above conclusion is also the same as False's judgment. Recommended tutorial:
PHP video tutorialThe above is the detailed content of PHP determines whether a variable is null. For more information, please follow other related articles on the PHP Chinese website!