Null (PHP)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Set a variable to null


$x = null;

Check for null + variations


Gewoon: is_null() [2]:

$myvar = NULL; is_null($myvar);  // True
$myvar = 0; is_null($myvar);     // False
$myvar = FALSE; is_null($myvar); // False
$myvar = '';  is_null($myvar);   // False
is_null($some_undefined_var);    // True, but the script throws a notice because $some_undefined_var doesn't exist



The function isset() checks whether a variable is defined (a.k.a exists) within your script. However, isset() also sees variables that have the null value as being not set (if you take the function wording literally).

This can be a little confusing but a few examples will show you exactly what I mean:

$myvar = NULL; isset($myvar);  // False
$myvar = 0; isset($myvar);     // True
$myvar = FALSE; isset($myvar); // True
$myvar = '';  isset($myvar);   // True
isset($some_undefined_var);    // False


The function empty() checks whether a variable evaluates to what PHP sees as a "falsy"(a.k.a empty) value. This being said, a variable is empty if it's undefined, null, false, 0 or an empty string.

To better understand the way empty works... you should think about this function as being the same as: !isset($var) || $var==false. Examples:

$myvar = NULL; empty($myvar);  // True
$myvar = 0; empty($myvar);     // True
$myvar = FALSE; empty($myvar); // True
$myvar = '';  empty($myvar);   // True
empty($some_undefined_var);    // True