PHP – empty() vs isset() vs is_null() vs boolean check

PHP isset() and empty() are frequently used to check the values of variables. In some cases is_null is also used. Here is a table covering various frequently used scenarios and the return value from these functions. These have been tried on Ubuntu Linux with php version 5.5.9.

empty(), isset(), is_null() and boolean check
Variable empty($a) isset($a) is_null($a) $a==false
$a not defined true false true true
$a=null; true false true true
$a=""; true true false true
$a=" "; false true false false
$a="0"; true true false true
$a=0; true true false true
$a=array(); true true false true
$a=true; false true false false
$a=false; true true false true

empty() and isset()

Few points to note about empty() and isset()

  1. empty() return false for space string (" ").
  2. isset() return true for variable with false value but not variable with null value.

empty() and boolean check

When we do boolean check (if ($a==false) {}), its outcome is almost same as outcome of empty() for simple cases like string, numbers, etc. Few points to note:

  1. For boolean check on undefined variable php generates notice “Undefined variable”
  2. php SimpleXML return false to empty() check for ‘‘ but returns true for boolean check.
    <?php
    $xmlstring = '<a><b></b></a>';
    $a = simplexml_load_string('<a><b></b></a>');
    var_dump(empty($a));
    var_dump($a == false);
    ?>
    bool(false)
    bool(true)
    
    Env: PHP 5.5.9 (Linux)

is_null() vs isset()

is_null() outcome is observed to be pretty much opposite of isset(). One observed difference is php generates notice “Undefined variable” when is_null() is used with undefined variable.

Demo code

Here is demo code with outcome:

<?php
function dump_four($code) {
  echo "$code\n";
  eval($code);
  echo 'empty($a)   => '; var_dump(empty($a));
  echo 'isset($a)   => '; var_dump(isset($a));
  echo 'is_null($a) => '; var_dump(is_null($a));
  echo '$a==false   => '; var_dump($a==false);
  echo "\n";
}
dump_four('$a;');
dump_four('$a=null;');
dump_four('$a="";');
dump_four('$a=" ";');
dump_four('$a="0";');
dump_four('$a=0;');
dump_four('$a = array();');
dump_four('$a=true;');
dump_four('$a=false;');
?>
$a;
empty($a)   => bool(true)
isset($a)   => bool(false)
is_null($a) => bool(true)
$a==false   => bool(true)

$a=null;
empty($a)   => bool(true)
isset($a)   => bool(false)
is_null($a) => bool(true)
$a==false   => bool(true)

$a="";
empty($a)   => bool(true)
isset($a)   => bool(true)
is_null($a) => bool(false)
$a==false   => bool(true)

$a=" ";
empty($a)   => bool(false)
isset($a)   => bool(true)
is_null($a) => bool(false)
$a==false   => bool(false)

$a="0";
empty($a)   => bool(true)
isset($a)   => bool(true)
is_null($a) => bool(false)
$a==false   => bool(true)

$a=0;
empty($a)   => bool(true)
isset($a)   => bool(true)
is_null($a) => bool(false)
$a==false   => bool(true)

$a = array();
empty($a)   => bool(true)
isset($a)   => bool(true)
is_null($a) => bool(false)
$a==false   => bool(true)

$a=true;
empty($a)   => bool(false)
isset($a)   => bool(true)
is_null($a) => bool(false)
$a==false   => bool(false)

$a=false;
empty($a)   => bool(true)
isset($a)   => bool(true)
is_null($a) => bool(false)
$a==false   => bool(true)

Env: PHP 5.5.9 (Linux)

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments