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

The difference between empty() and isset() functions

WBOY
WBOYOriginal
2016-07-25 09:11:461317browse

This article introduces the difference between empty and isset functions in PHP. Friends who are interested can refer to it. When writing page programs in PHP, I often use variable processing functions to determine whether a variable value at the end of the PHP page is empty. At first, I was used to using the empty() function, but I found some problems, so I switched to isset. () function, the problem is no longer there.

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 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 detect whether the variable $id has been configured. Both will return different values ​​- empty() thinks there is no configuration, isset() Can get the value of $id:

  1. $id=0;
  2. empty($id)?print "It's empty .":print "It's $id .";
  3. //Result: It's empty .
  4. print "
    ";
  5. !isset($id)?print "It's empty .":print "It's $id .";
  6. //Result: It's 0 .
Copy code

This means that when we use the variable processing function , when the variable may have a value of 0, be careful when using empty(). It is more sensible to replace it with isset at this time.

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

  1. if(empty($id)) $id=1; - If id=0, id will also be 1
  2. if(!isset($id)) $id=1; - If id=0 , the id will not be 1
Copy the code

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

  1. if(empty($id)) $id=1;

  2. print $id; // get 1

  3. if(!isset($id) ) $id=1;

  4. print $id; //Get 0

Copy code

  To talk about their connection, the common point is that empty() and isset() are both variable processing Functions are used to determine whether variables have been configured. It is precisely because of their great similarity in the process of processing variables that they have insufficient understanding of their relationship. Just considering the two functions empty() and isset() themselves 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 and isset() will return TRUE;

If the variable is not defined, empty() will return TRUE and isset() will return FLASE;

Explanation of empty() in the manual 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. Explanation of isset() in the manual isset() checks whether the variable is set Description bool isset ( mixed var [, mixed var [, ...]] ) Returns TRUE if var exists, FALSE otherwise. If a variable has been freed 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 (" Warning: isset() can only be used with variables, as passing any other arguments will cause a parsing error. If you want to check whether a constant has been set, you can use the defined() function. 1. When you want to determine whether a variable has been declared, you can use the isset function 2. When you want to determine whether a variable has been assigned data and is not empty, you can use the empty function 3. When you want to judge whether a variable exists and is not empty, first use the isset function and then use the empty function



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