Home >Backend Development >PHP Tutorial >Detailed explanation of the differences between empty, is_null and isset in php_PHP tutorial

Detailed explanation of the differences between empty, is_null and isset in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:271104browse

There are many functions with similar functions in PHP, but there are subtle differences. As analyzed in this article, the three functions of is_null, empty, and isset are not easy to implement without a lot of effort. Got it! Let’s follow the webmaster to learn more about the differences between these three functions!

Let’s first take a look at the functional descriptions of these three functions

isset determines whether the variable already exists. If the variable exists, it returns TRUE, otherwise it returns FALSE.

empty determines whether the variable is empty. If the variable is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty, and TRUE will be returned if the variable is empty.

is_null determines whether the variable is NULL

How about this? This explanation is generally used, but this explanation is already very confusing. Let’s analyze it with specific examples below!

Detailed explanation of the differences between empty, is_null and isset in php_PHP tutorial

From this we can find that as long as the variable is "" or 0, or false or null, empty will return true as long as these values ​​​​are.

isset only determines whether the variable exists. As long as your variable is not null or unassigned, the return result will be true. If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("

And is_null is exactly the inverse result of isset. We can think of it as !isset, which is an inverse operation of isset.

From the above examples, we can also draw the following conclusions (which will be often used in programming in the future):

Assume $var is any type

When empty($var) is true, (bool)($var) is false. vice versa.

When is_null($var) is true, isset($var) is false. vice versa.

For example:

$i=$j+1;

Here is_null($j) is true (it can be understood that isset($j) is false, because the variable $j is not declared in advance)

Two other points to note are :

(1) empty() only detects variables, and detecting anything that is not a variable will result in a parsing error. In other words, the following statement will not work: empty(addslashes($name)).

(2) isset() can only be used for 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.

Articles you may be interested in

    The usage and difference of using break, continue, goto, return, exit to break out of multiple loops in PHP
  • tinyint, The usage differences between smallint, int and bigint types
  • The usage and differences between echo, print, print_r, var_export, var_dump in PHP
  • The difference between execute and query methods in ThinkPHP
  • The difference between readonly, disabled, display and visible
  • A brief explanation of the difference between scrollHeight, scrollWidth, scrollLeft, scrolltop, etc. in javascript
  • The difference between PHP merge array+ and array_merge
  • php Usage of keywords such as $this, static, final, const, self etc.

http://www.bkjia.com/PHPjc/764073.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764073.htmlTechArticleThere are many functions with similar functions in PHP, but there are subtle differences. As analyzed in this article, is_null, The three functions of empty and isset are not easy to implement without a lot of effort...
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