Home  >  Article  >  Backend Development  >  The difference between PHP isset() and empty()

The difference between PHP isset() and empty()

不言
不言Original
2018-05-08 16:34:061615browse

This article mainly introduces the difference between PHP isset() and empty(). It has certain reference value. Now I share it with you. Friends in need can refer to it.

When writing in PHP When programming a page, I often use the variable processing function to determine whether a variable value at the end of the PHP page is empty. At the beginning, I was used to using the empty() function, but found some problems, so I switched to the isset() function. Problem no more.

As the name suggests, empty() determines whether a variable is "empty", and isset() determines whether a variable has been set. It is this so-called "as the name implies" that made me take some detours at the beginning: when a variable value is equal to 0, empty() will also be true (True), so some accidents will occur. It turns out that although empty() and isset() are both variable processing functions, they are both used to determine whether the variable has been configured, but they have certain differences: empty will also detect whether the variable is empty or not. zero. When a variable value is 0, empty() considers the variable to be equivalent to being empty, which is equivalent to not being set.

For example, to detect the $id variable, when $id=0, use empty() and isset() to check whether the variable $id has been configured. Both will return different values ​​- — empty() considers that there is no configuration, isset() can get the value of $id:

 $id=0;
 empty($id)?print "It's empty .":print "It's $id ."; 
  //结果:It's empty .
 print "
";
 !isset($id)?print "It's empty .":print "It's $id .";
  //结果:It's 0 .

This means that when we use the variable processing function, when This variable may have a value of 0, so be careful when using empty(), in which case it would be wiser to replace it with isset.

When the URL tail parameter of a php page appears id=0 (for example: test.php?id=0), try to compare:

if(empty($id)) $id=1; - 若 id=0 ,id 也会为1
if(!isset($id)) $id=1; - 若 id=0 ,id 不会为1

You can run the following code separately to detect the above inference:

 if(empty($id)) $id=1;
 print $id; // 得到 1
 if(!isset($id)) $id=1;
 print $id; //得到 0

To talk about their connection, their common point is that both empty() and isset() are The function of the variable processing function is to determine whether the variable has been configured. It is precisely because of their great similarity in the process of processing variables that they have insufficient understanding of their relationship. If you only consider the two functions empty() and isset() themselves, it will make people more confused. Look at it from another angle. The processing objects of empty() and isset() are nothing more than undefined variables, 0, and empty strings.

If the variable is 0, empty() will return TRUE and isset() will return TRUE;

If the variable is an empty string, empty() will return TRUE, isset() will return TRUE;

If the variable is undefined, empty() will Returns TRUE, isset() will return FLASE;

The explanation of empty() in the manual is as follows:

Description bool empty(mixed var)

If var 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 properties will be considered empty, and TRUE is returned if var is empty.

The explanation of isset() in the manual is as follows:

isset() detects whether the variable is set

Description bool isset (mixed var [, mixed var [, ...]] )

Returns TRUE if var exists, otherwise returns 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, you can use the defined() function.

When you want to determine whether a variable has been declared, you can use the isset function

## When you want to judge whether a variable has been assigned data and is not empty, you can use the empty function

#When you want to judge whether a variable exists and is not empty, first use the isset function and then empty function

Related recommendations:

Analysis of the difference between php://output and php://stdout

The above is the detailed content of The difference between PHP isset() and empty(). 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