The difference between using PHP isset of and empty of

  • 2020-03-31 21:05:37
  • OfStack

PHP's isset () function is generally used to detect whether a variable isset or not
Format: bool isset (mixed var [, mixed var [,...]])

Function: detect whether the variable is set

The return value:

Returns FALSE if the variable does not exist
Also returns FALSE if the variable exists and its value is NULL
True is returned if the variable exists and the value is not NULL
When multiple variables are checked at the same time, each item will only return TRUE if it meets the previous requirement; otherwise, the result will be FALSE
Version: PHP 3, PHP 4, PHP 5
More details:
Once the variable is freed with unset(), it is no longer an isset().
The PHP function isset() can only be used for variables, and passing any other arguments will cause a parsing error.
Detects whether a constant is set to use the defined() function.

PHP's empty () function determines whether the value is empty

Format: bool empty (mixed var)

Function: checks if a variable is empty

The return value:

Returns TRUE if the variable does not exist
If the variable exists and its value is "", 0, "" 0", NULL, FALSE, array(), var $var; And an object without any properties, true is returned
If the variable exists and the value is not "", 0, "" 0", NULL, FALSE, array(), var $var; And objects without any properties, return FALSE
Version: PHP 3, PHP 4, PHP 5
More details:
Return value of empty() =! (Boolean) var, but does not generate a warning because the variable is undefined. See converting to Boolean values for more information.
Empty () can only be used for variables, and passing any other parameters will cause a Paser error to terminate the run.
Detects whether a constant is set to use the defined() function.
Example: a simple comparison between empty() and isset()
 
<?php 
$var = 0; 
//The result is true because $var is empty
if (empty($var)) { 
echo '$var is either 0 or not set at all'; 
} 
//The result is false because $var is set
if (!isset($var)) { 
echo '$var is not set at all'; 
} 
?> 

Note: since this is a language structure and not a function, it cannot be called by a variable function.
Note: empty() only detects variables, and anything that is not a variable will cause a parsing error. In other words, the following statement will not work: empty(addslashes($name)).
The following is a section of isset and empty function after the site tested the detailed example of the code, see this is basically the same:

<?php 
error_reporting(E_ALL); 
echo '<B> undefined $var</b><Br>'; 
echo "isset test :<Br>"; 
if ( isset ( $var )) 
{ 
echo ' variable $var There are !<Br>' ; 
} 
echo "empty test :<Br>"; 
if ( empty ( $var )){ 
echo ' variable $var The value is empty <Br>'; 
} 
else 
{ 
echo ' variable $var Is not null <Br>'; 
} 
echo " Variable direct test :<Br>"; 
if ( $var ){ 
echo ' variable $var There are !<Br>'; 
} 
else { 
echo ' variable $var There is no !<Br>'; 
} 
echo '----------------------------------<br>'; 
echo '<B>$var = ''</b><Br>'; 
echo "isset test :<Br>"; 
$var = ''; 
if ( isset ( $var )) 
{ 
echo ' variable $var There are !<Br>' ; 
} 
echo "empty test :<Br>"; 
if ( empty ( $var )){ 
echo ' variable $var The value is empty <Br>'; 
} 
else 
{ 
echo ' variable $var Is not null <Br>'; 
} 
echo " Variable direct test :<Br>"; 
if ( $var ){ 
echo ' variable $var There are !<Br>'; 
} 
else { 
echo ' variable $var There is no !<Br>'; 
} 
echo '----------------------------------<br>'; 
echo '<B>$var = 0</b><Br>'; 
echo 'isset test :<Br>'; 
$var = 0 ; 
if ( isset ( $var )) 
{ 
echo ' variable $var There are !<Br>' ; 
} 
echo "empty test :<Br>"; 
if ( empty ( $var )){ 
echo ' variable $var The value is empty <Br>'; 
} 
else 
{ 
echo ' variable $var Is not null <Br>'; 
} 
echo " Variable direct test :<Br>"; 
if ( $var ){ 
echo ' variable $var There are !<Br>'; 
} 
else { 
echo ' variable $var There is no !<Br>'; 
} 
echo '----------------------------------<br>'; 
echo '<B>$var = null</b><Br>'; 
echo 'isset test :<Br>'; 
$var = null ; 
if ( isset ( $var )) 
{ 
echo ' variable $var There are !<Br>' ; 
} 
echo "empty test :<Br>"; 
if ( empty ( $var )){ 
echo ' variable $var The value is empty <Br>'; 
} 
else 
{ 
echo ' variable $var Is not null <Br>'; 
} 
echo " Variable direct test :<Br>"; 
if ( $var ){ 
echo ' variable $var There are !<Br>'; 
} 
else { 
echo ' variable $var There is no !<Br>'; 
} 
echo '----------------------------------<br>'; 

echo '<B>$var ="php"</b><Br>'; 
echo 'isset test :<Br>'; 
$var = "php"; 
if ( isset ( $var )) 
{ 
echo ' variable $var There are !<Br>' ; 
} 

echo "empty test :<Br>"; 
if ( empty ( $var )){ 
echo ' variable $var The value is empty <Br>'; 
} 
else 
{ 
echo ' variable $var Is not null <Br>'; 
} 
echo " Variable direct test :<Br>"; 
if ( $var ){ 
echo ' variable $var There are !<Br>'; 
} 
else { 
echo ' variable $var There is no !<Br>'; 
} 
?>


When writing page programs in PHP, I often use variable handling functions to determine whether a variable value of a parameter at the end of a PHP page is empty. At first, I used to use the empty() function, but I found some problems.
As the name suggests, empty() determines whether a variable is "empty" and isset() determines whether a variable has been set. It was this "as the name suggests" that got me off on the wrong foot: empty() also holds (True) when a variable's value is equal to 0, so something unexpected happens. It turns out that although both empty() and isset() are variable handlers that are used to determine whether a variable has been configured, they are somewhat different: empty also checks if a variable is empty or zero. When the value of a variable is 0, empty() thinks that the variable is equal to empty, which means there is no setting.
For example, 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, and isset() can get the value of $id:

 
$id=0; 
empty($id)?print "It's empty .":print "It's $id ."; 
//Result: It's empty.
print "<br>"; 
!isset($id)?print "It's empty .":print "It's $id ."; 
//Result: It's 0.


This means that when we use a variable handler, when it might have a value of 0, we should be careful with empty(), and it would be wiser to replace it with an isset.
When id=0 appears at the end of the URL of a PHP page (e.g., test.php? Id =0), try to compare:

 
if(empty($id)) $id=1; -  if  id=0  . id  Will be 1 
if(!isset($id)) $id=1; -  if  id=0  . id  Not for 1 


The above inference can be detected by running the following code separately:

 
if(empty($id)) $id=1; 
print $id; //Get 1
if(!isset($id)) $id=1; 
print $id; //Get zero


In terms of their connection, the common point is that both empty() and isset() are variable handling functions, which are used to judge whether variables have been configured or not. It is because of their great similarity in the process of handling variables that their relationship is not fully understood. Thinking in terms of the empty() and isset() functions themselves can confuse you even more. The empty() and isset() objects are nothing more than undefined variables, 0, empty strings.
If the variable is 0, empty() returns TRUE, and isset() returns TRUE.

If the variable is an empty string, empty() returns TRUE, and isset() returns TRUE.
If the variable is undefined, empty() returns TRUE, and isset() returns FLASE.

The manual explains empty() as follows:

Describe bool empty(mixed var)
Empty () returns FALSE if the var is a non-empty or non-zero value. In other words, "", 0," 0, NULL, FALSE, array(), var $var; And objects without any attributes will be considered empty, and if the var is empty, TRUE will be returned.
The manual explains isset() as follows:

Isset () detects whether a variable isset

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

Var returns TRUE if it exists, or FALSE if it does not.

If a variable has been freed using unset(), it will no longer be an isset(). Using isset() to test a variable that isset to NULL returns FALSE. Also note that a NULL byte ("? ) is not equivalent to PHP's NULL constant.
Warning: isset() can only be used with variables, because passing any other arguments will cause a parsing error. To detect if a constant is set, use the defined() function.

The isset function is used when determining whether a variable has been declared
The empty function is used when determining whether a variable has been assigned data and is not empty
When a variable exists and is not empty, the isset function is used before the empty function

Related articles: