Home >Backend Development >PHP Problem >PHP determines whether a variable is null

PHP determines whether a variable is null

王林
王林Original
2019-09-24 17:54:434293browse

PHP determines whether a variable is null

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 tutorial

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php shield errorNext article:php shield error