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

isset() Function

The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.

Syntax:

bool isset( $var, mixed )

Parameters: This function accepts more than one parameters. The first parameter of this function is $var. This parameter is used to store the value of variable.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate 
// isset() function
$num = '0';
  
if( isset( $num ) ) {
    print_r(" $num is set with isset function <br>");
}
  
// Declare an empty array 
$array = array(); 
    
// Use isset function 
echo isset($array['geeks']) ? 
'array is set.' 'array is not set.'
?>

chevron_right


Output:

0 is set with isset function 
array is not set.

empty() Function

The empty() function is a language construct to determine whether the given variable is empty or NULL. The !empty() function is the negation or complement of empty() function. The empty() function is considerably equal to !isset() function and !empty() function is equal to isset() function.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate
// empty() function
  
$temp = 0;
  
// It returns true because 
// $temp is empty
if (empty($temp)) {
    echo $temp . ' is considered empty';
}
  
echo "\n";
  
// It returns true since $new exist
$new = 1;
if (!empty($new)) {
    echo $new . ' is considered set';
}
?> 

chevron_right


Output:

0 is considered empty
1 is considered set

Reason to check both function:
The isset() and !empty() functions are similar and both will return the same results. But the only difference is !empty() function will not generate any warning or e-notice when the variable does not exists. It is enough to use either of the function. By incorporating both functions in a program causes time lapse and unnecessary memory usage.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP function to demonstrate isset()
// and !empty() function
  
// Inintialize a variable
$num = '0';
  
// Check isset() function
if( isset ( $num ) ) {
    print_r( $num . " is set with isset function");
}
  
// Display new line
echo "\n";
  
// Inintialize a variable
$num = 1;
  
// Check the !empty() function
if( !empty ( $num ) ) {
    print_r($num . " is set with !empty function");
}

chevron_right


Output:

0 is set with isset function
1 is set with !empty function


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.