In PHP check isset of and! The Necessity of empty of Function

  • 2021-11-24 01:08:58
  • OfStack

The isset () function is a built-in function in PHP that checks whether the variable is set and is not NULL. This function also checks whether the declared variable, array, or array key has a null value, and if so, isset () returns false, which returns true all other possible.

Syntax:


bool isset( $var, mixed )

Arguments: This function accepts multiple arguments. The first argument to this function is $var. This parameter is used to store the value of the variable.

Example:


<?php 
  
$num = '0'; 
  
if( isset( $num ) ) { 
  print_r(" $num is set with isset function <br>"); 
} 
  
//  Declaration 1 An empty array  
$array = array(); 
   
echo isset($array['geeks']) ? 
'array is set.' : 'array is not set.'; 
?>

Output:


0 is set with isset function
array is not set.

The empty () function is a language construct that determines whether a given variable is empty or NULL. ! The empty () function is a negation or supplement of the empty () function. The empty () function is the same as! The isset () function is equivalent, and! The empty () function is equal to the isset () function.

Example:


<?php 
  
  
$temp = 0; 
  
if (empty($temp)) { 
  echo $temp . ' is considered empty'; 
} 
  
echo "\n"; 
  
$new = 1; 
if (!empty($new)) { 
  echo $new . ' is considered set'; 
} 
?>

Output:


0 is considered empty
1 is considered set

Check the reasons for two functions:

isset () and! The empty () function is similar, and both will return the same result. But the only difference between 1 is! When the variable does not exist, the empty () function does not generate any warnings or electronic notifications. It is enough to use any 1 function. By merging the two functions into the program, time elapses and unnecessary memory usage will occur.

Example:


<?php 
 
$num = '0'; 
  
if( isset ( $num ) ) { 
  print_r( $num . " is set with isset function"); 
} 
  
echo "\n"; 
  
$num = 1; 
  
if( !empty ( $num ) ) { 
  print_r($num . " is set with !empty function"); 
}

Output:


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

These are all the knowledge points introduced this time. Thank you for your support to this site.


Related articles: