isset() and NULL

The unexpected behaviour of isset() for variables which have the value NULL caused a bit of a headache today. One would assume if a variable has any value assigned testing this variable with isset() would return true. This is the case except when the variable has the value NULL assigned. Here a quick example:

$a = 1;
var_dump(isset($a));
$a = null;
var_dump(isset($a));

This outputs:

bool(true)
bool(false)

The PHP documentation for isset() states:

isset() will return FALSE if testing a variable that has been set to NULL.

Kunststube.net has a very good overview on isset() and empty() in combination with NULL .

I'm available for contracting work. Check out my LinkedIn profile and my portfolio page for an overview of my skills and experience. If you are interested in working with me please use the contact form to get in touch.

isset() and NULL

Leave a Reply

Your email address will not be published. Required fields are marked *