Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between isset(), empty() and is_null() functions

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 385
    Comment on it

    In PHP we have three different functions which can be used to test the value of a variable. They are isset(), empty() and is_null(). All these functions return a boolean value. But in case if these functions are not used in correct way they can cause unexpected results.

     

    Isset() - this function is used to check whether a variable is set or not and that is not null, i.e., this function will return boolean true only in a case where variable is not null.

     

    Empty() - this function, as the name suggests, will return boolean true if the variable is an empty string, false, array(), NULL and an unset variable.

     

    is_null() - this function is opposite to the isset() function. This function is used to find whether a variable is NULL. One difference between the isset() and is_null() function is that former can be applied to unknown variables, but is_null() only to declared variables.

     

    Table for reference what these functions will return for different values.

    Value of variable($var)

    isset($var)

    empty($var)

    is_null($var)

    FALSE

    bool(true)

    bool(true)

     
    TRUE

    bool(true)

       
    array() (an empty array)

    bool(true)

    bool(true)

     
    "" (an empty string)

    bool(true)

    bool(true)

     
    "  " (space)

    bool(true)

       
    NULL  

    bool(true)

    bool(true)

    "0" (0 as a string)

    bool(true)

    bool(true)

     
    0 (0 as an integer)

    bool(true)

    bool(true)

     
    0.0  (0 as a float)

    bool(true)

    bool(true)

     
    var $x (declared variable but without a value)  

    bool(true)

    bool(true)

    NULL byte ("\0")

    bool(true)

       

     

    /* Program to show the functionality of isset() */

    
    <?php
    	$var= ' ';
    	if(isset($var))    // It will evalute to true so the text will be printed.
    	{
    		echo This variable is set to true.;
    	}
    	
    	$x = sample_data;
    	$y = another sample_data;
    	var_dump(isset($x));     	  //TRUE
    	var_dump(isset($x, $y)); 	 //TRUE
    	
    	unset($x);
    
    	var_dump(isset($x)); 		//FALSE
    	var_dump(isset($x, $y));	//FALSE
    
    	$arr = array('array1' => 1, 'array2' => NULL, 'array3' =>array('sub_array' => 'Sarray'));
    	
    	var_dump(isset($arr['array1']));	//TRUE
    	var_dump(isset($arr['array2']));	//FALSE ; as the key array2 equals NULL so it is 						   considered unset.
    	
    	var_dump(isset($arr['array3']['sub_array]));		//TRUE
    
    ?>
    

     

    /* Program to demonstrate the comparison between isset() and empty() function */

    <?php
    
    	$v = 0;
    	
    	if(empty($v))			// Evaluates to true as $v is empty
    	{
    		echo $v is either 0, empty, or is not set;
    	}
    
    	if(isset($v))			// Evaluates to true as $v is set
    	{
    		echo  $v is set and empty ;
    	}
    ?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: