Why to check both isset() and !empty() function in PHP ?



Not Found
404
Not Found

Php offers some magical predefined function to handle the variables in real-time applications.In this article we will study about isset() and !empty() function and implementation both of these functions with few examples.

isset():

The isset() function is a predefined function in PHP which checks whether a variable is declared in the application and is not assigned NULL. This function restores the outcome as true or false.

Let's test this with an example.

<?php
   $var1 = NULL;
   $var2 = 1;
   var_dump(isset($var1));
   var_dump(isset($var2));
?>

Output:

bool(false)
bool(TRUE)

Explanation:

In the above example $var1 is defined as equal to NULLi.e $var1 = NULL,when isset() function performed on $var1, this will evaluate as FALSE,because $var1 is assigned to NULL. In the second case $var2 is defined as equal to zero 1 i.e $var2 = 1,when isset() function performed on $var2,this this will evaluate as TRUE,becuase $var1 is not null.

In the event that if more than one parameters are provided, then isset() will return TRUE only if all of the parameters are treated as a set. Assessment goes from left to right and stops when an undefined/unset variable is experienced.

Let's test the above with an example.

<?php
   $a = "10";
   $b = "20";
   var_dump(isset($a));
   var_dump(isset($a, $b));
   var_dump(isset($c,$a, $b));
?>

Output:

bool(true)
bool(true)
bool(false)

Explanation:

Here we have declared $a and $b variable and assign some value i.e $a ="10" and $b = "20" but $c is not defined. The first expression evaluates to true because $a is defined, The second expression is also evaluated to true because both $a and $b are defined. The next expression evaluates false because $c is not defined.

Let's learn about empty().

empty():

The empty() function decides if the given variable is empty or NULL. The !empty() function is the supplement of empty() function. The empty() function is significantly equivalent to equal to !isset() function and !empty() function is equal to isset() function. It displays the outcome as TRUE or FALSE. Let's discuss with this example.

<?php
   $var1 = 0;
   var_dump(empty($var1)) ;
   var_dump(!empty($var1)) ;
?>

Output:

bool(TRUE)
bool(FALSE)

Explanation :

Here $var1 is defined as equals to 0 i.e $var1 =0 ,when we perform empty() function over this,it will evaluate as TRUE because 0 is considerd as empty. However !empty() function evalute false which is equal to isset() function result.

To check both functions:

The isset() and !empty() functions produced same results as long as functionality is considered. But the only difference is !empty() function will not generate any warning or E_Notice when the variable does not exist.

Note:

The following values are considered to be empty:

  • " " (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)


raja
Updated on 30-Jul-2019 22:30:26

Advertisements