Null vs. isset()

Wed, Jan 28, 2009 02:46 PM

I am working with a newcomer to PHP and he asked me about setting a variable to null and how to check that.  He had found some example or information that showed that setting a varaible equal to null would unset the variable.  So, he was unclear if he could then reliably check if the variable was equal to null.  Having avoided null like the plague in my years of PHP, I was not sure.  So, I mocked up a quick script to see what the states of a variable are in relation to null.

<?php

error_reporting(E_ALL);

$null = null;

if($null == null){
echo "Yes, it is equivilent to null\n";
}

if($null === null){
echo "Yes, it is null\n";
}

if(isset($null)){
echo "Yes, it is set\n";
}

if(empty($null)){
echo "Yes, it is empty\n";
}

?>

The output of this script is:

Yes, it is equivilent to null
Yes, it is null
Yes, it is empty

It should also be noted that without declaring the variable, you will see PHP notices about an undefined variable.  So, there is a slight difference between and unset variable and a variable that has been set to null.


12 comments
Gravatar for Juan Juarez

Juan Juarez Says:

Keeping track of the different comparison operators can be confusing. I often find myself referring to this chart : http://www.php.net/manual/en/types.comparisons.php

Gravatar for Eric

Eric Says:

Why not using is_null? http://www.php.net/is_null

Greetings
Eric

Gravatar for Mazzu

Mazzu Says:

To check if the variable doesn't exist and to not be confused with null, you may use :
- for key of array : array_key_exists($key, $array)
- for property of object : property_exists($property)
- for global variable : array_key_exists('variable', $GLOBALS)
- for local variable : I don't know

Gravatar for Janci

Janci Says:

Re Mazzu: for a local variable you could test against the array returned by get_defined_vars() function like this: array_key_exists('foo', get_defined_vars());

get_defined_vars() returns a multi-dimensional array containing all defined variables within the scope. Note that if called on global level (i.e. outside a function/method), it will also return all global variables. More info: http://uk.php.net/manual/en/function.get-defined-vars.php

Gravatar for Sjon

Sjon Says:

The main difference between an unset variable and a null variable is that the unset variable doesn't exist in the current scope, whereas the null variable is a defined variable with no value (because null is essentially the i-have-no-value value).
The isset() function is in that way confusing because it not only checks whether the variable is defined in the current scope, but also if it has a value which is not null.
The is_null() function is indeed the best way to determine if a variable has a null value, or, if you must, the === operator. The empty() function is not a good way, because it will also return true for empty strings and a zero.

Regards,

Gravatar for Janci

Janci Says:

Sjno, unfortunatelly, there's not much difference between using isset() and is_null() as these two functions are reverse (i.e. if isset() returns true, is_null() returns false and vice versa). The only difference is that if passing non-existing/unset variable to is_null(), it generates a notice (but still returns true).

As I said in my previous post, it seems that the only way to find out whether a variable is actually set (even if null) is to use the get_defined_vars().

Gravatar for Janci

Janci Says:

Sjon, sorry for misspelling your nick, dude.

Gravatar for Sjon

Sjon Says:

@Janci,
I know it may look that way, but they are not really each other's opposites. I think empty() would be a better fit for isset(). Both those functions can be used to check for a/no value, whereas the is_*() family is used to check for a certain type of variable.

Using get_defined_vars() may be the best way to determine if a variable is defined, but because of the array search also slow(er). Besides, if you really need to check if a variable is set, even if null, I think you're looking at a weird/wrong solution to your problem (at least, I can't think of any good reason why. If you've got a good example, please share).

A good example of how to use null values and is_null is something like this (in quickly mocked up pseudocode):

<?php

$name = $email = null;
if (isset($_POST['name']) && isValid($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'] && isValid($_POST['email'])) {
$email = $_POST['email'];
}
if (is_null($name) || is_null($email)) {
displayMissingRequiredInput();
}
else {
processInput();
}

?>

Oh, and about the misspelling, no worries... :-)

Regards,

Gravatar for Sjon

Sjon Says:

(rest of my post)

$name = $email = null;
if (isset($_POST['name']) && isValid($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'] && isValid($_POST['email'])) {
$email = $_POST['email'];
}
if (is_null($name) || is_null($email)) {
displayMissingRequiredInput();
}
else {
processInput();
}

Oh, and about the misspelling, no worries... :-)

Regards,

Gravatar for Janci

Janci Says:

@Sjon: I do completely agree with all you've said. From the practical point of view, using get_defined_vars() means somethings rotten in your code. I was just trying to point out the tricks&catches when working with nulls in PHP.

Gravatar for Winer Reales

Winer Reales Says:

Sure is best to use
if (is_null($name) || is_null($email))
in PHP5 you can't use isset() for variable only for http request, post or get

Comments are disabled for this post.