InfoHeap
Tech tutorials, tips, tools and more
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);
    ?>
    Env: PHP 7.3.18 (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.3.18 (Linux)

Suggested posts:

  1. Git – Check if a hash is valid
  2. Javascript check if variable is defined
  3. php get array value with default
  4. How to return a value from bash function
  5. Python filter list/iterable examples
  6. CSS :empty – define style for empty element
  7. Bash – set default value if a variable is empty
  8. PHP – check if a value is in array
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

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | 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

Copyright © 2023 InfoHeap.

Powered by WordPress