InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search
  • Home
  • > Tutorials
  • > PHP cookbook

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

By admin on Dec 12, 2015

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(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)

Suggested posts:

  1. PHP – check if a value is in array
  2. Bash – iterate over array
  3. Git show details of a commit hash
  4. CSS :only-of-type – define style of only child a type
  5. How to specify environment variable for a command on Linux
  6. Javascript – iterate over function arguments
  7. Can ssh keys generated as one user be used as a different user
  8. Javascript array forEach
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, PHP, PHP cookbook, Tutorials, Ubuntu Linux, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress