Home  >  Article  >  Backend Development  >  php determines whether it is null

php determines whether it is null

王林
王林Original
2019-09-25 11:54:1210989browse

php determines whether it is null

php determines whether a variable is NULL

There are many ways to determine whether a variable is NULL in PHP:

is_null or isset

Both functions can 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.

== or ===

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. In some scenarios, it is inappropriate to use isset, such as checking whether the return value of a function is NULL. At this time, you can use "==" and "====" to determine whether they are NULL.

For "==" and "===", they are directly different

For "==", it recognizes the empty string, 0, false are all 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 consistent with False's judgment.

Recommended tutorial: PHP video tutorial

The above is the detailed content of php determines whether it 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