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.
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()
- empty() return false for space string (
" "
). - isset() return true for variable with
false
value but not variable withnull
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:
- For boolean check on undefined variable php generates notice “Undefined variable”
- 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(false)
Env: PHP 7.4.33 (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 7.4.33 (Linux)