jump to navigation

PHP’s isset() vs empty() vs is_null() September 24, 2012

Posted by Tournas Dimitrios in PHP.
trackback

PHP has over a dozen of “Variable Evaluation Functions” , where almost all have an obvious (self explanatory) functionality , three of these might be confusing and produce unexpected results when used into conditional statements . As the title of this article reveals what functions I’m talking about  let’s get straight to the subject .

  • isset()  :  Evaluates if a variable is set , has already a value assigned to it  and is not NULL . Notice that this function produces slightly different results on PHP 5.3 and PHP 5.4
  • empty() :  Evaluates whether a variable is empty . Notice the evaluation of Boolean values (TRUE-FALSE) . A variable is considered empty if it does not exist or if its value equals FALSEempty() does not generate a warning if the variable does not exist . Also notice that this function produces slightly different results on PHP 5.3 and PHP 5.4 .
  • is_null() :  Returns TRUE if var is NULL , FALSE otherwise .

The table below is a quick reference for what these functions will return for different values (PHP Version 5.3.10)  .

Value of variable ($var) isset($var) empty($var) is_null($var)
Variable isn’t declared at all bool(false) bool(true) bool(true)
NULL bool(false) bool(true) bool(true)
var $var; (a variable declared ,
but without a value)
bool(false) bool(true) bool(true)
“” (an empty string) bool(true) bool(true) bool(false)
FALSE bool(true) bool(true) bool(false)
array() (an empty array) bool(true) bool(true) bool(false)
“0” (0 as a string) bool(true) bool(true) bool(false)
0 (0 as an integer) bool(true) bool(true) bool(false)
0.0 (0 as a float) bool(true) bool(true) bool(false)
NULL byte (“\ 0”) bool(true) bool(false) bool(false)
” ” (space) bool(true) bool(false) bool(false)
TRUE bool(true) bool(false) bool(false)

Above table was produced with the following code :

<?php

ini_set('display_errors' , 0 ) ;
echo '<table border="2">';
echo '<tr><th>Value of variable ($var)</th><th>isset($var)</th><th>empty($var)</th><th>is_null($var)</th></tr>';
echo '<tr><td>Variable isn\'t declared at all</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = NULL;
echo '<tr><td>NULL</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
unset($var) ;
$var;
echo '<tr><td>var $var; (a variable declared ,<br> but without a value)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = '';
echo '<tr><td>"" (an empty string)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = FALSE;
echo '<tr><td>FALSE</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = array();
echo '<tr><td>array() (an empty array)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = '0';
echo '<tr><td>"0" (0 as a string)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = 0;
echo '<tr><td>0 (0 as an integer)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = 0.0;
echo '<tr><td>0.0 (0 as a float)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = '\0';
echo '<tr><td>NULL byte ("\ 0")</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = ' ';
echo '<tr><td>" " (space)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
$var = TRUE;
echo '<tr><td>TRUE</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';
echo '</table>';

Happy coding 🙂  .

Advertisements

Comments»

1. linda - October 5, 2012

your statement was highly appreciating i will make a look on.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s