Friday, 20 May 2016

What is the difference between isset and empty in PHP

Isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
Returns TRUE if var exists; FALSE otherwise.

empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.

Examples
 function checkIfIsset($value){
      if (isset($value)) {
        return "It is set";
      } else {
        return "It is not set";
      }
 }
 function checkIfnotEmpty($value){  
      if (!empty($value)) {  
        return "It is not empty";  
      } else {  
        return "It is empty";  
      }  
 }  
Examples of Isset method.
Exampl 1: Lets set $value to False and call function checkIfIsset as below

 $value = False;  
 echo checkIfIsset($value);  
Out put: It is set
Example 2: Lets set $value to NULL and call function checkIfIsset as below
 $value = Null;  
 echo checkIfIsset($value);  
Out put: It is not set
Examples of Empty method.
Example 1: Lets set $value to empty string and call function checkIfnotEmpty as below
 $value = '';  
 echo checkIfnotEmpty($value);  
Out put: It is empty
Example 2: Lets set $value to zeo and call function checkIfnotEmpty as below
 $value = 0;  
 echo checkIfnotEmpty($value);  
Out put: It is not empty

Reference Link: http://whatisdifference.com/what-is-the-difference-between-isset-and-empty-in-php/

2 comments: