How to tell whether a variable is null or undefined in php
Using modified example from PHP - Differenciate an undefined variable from a null variable
we can differentiate it:
$v1 = null;
echo (isset($v1) ? '$v1 set' : '$v1 not set') . PHP_EOL;
echo (is_null($v1) ? '$v1 null' : '$v1 not null') . PHP_EOL;
echo (empty($v1) ? '$v1 empty' : '$v1 not empty') . PHP_EOL;
echo (array_key_exists('v1', get_defined_vars()) ? '$v1 defined' : '$v1 not defined') . PHP_EOL;
echo PHP_EOL;
echo (isset($v2) ? '$v2 set' : '$v2 not set') . PHP_EOL;
echo (@is_null($v2) ? '$v2 null' : '$v2 not null') . PHP_EOL;
echo (empty($v2) ? '$v2 empty' : '$v2 not empty') . PHP_EOL;
echo (array_key_exists('v2', get_defined_vars()) ? '$v2 defined' : '$v2 not defined') . PHP_EOL;
prints:
$v1 not set
$v1 null
$v1 empty
$v1 defined
$v2 not set
$v2 null
$v2 empty
$v2 not defined
we can use array_key_exists(..., get_defined_vars())
and is_null(...)
to detect both situations
You can't wrap this kind of logic in a function or method as any variable defined in a function signature will be implicitly "set". Try something like this (contains code smell)
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
error_reporting(E_ALL);
set_error_handler("exception_error_handler");
try {
if (null === $var) {
// null your variable is, hmmm
}
} catch (ErrorException $e) {
// variable is undefined
}
In PHP typically variables that have not been set or that have been unset are considered null
. The meaning of null
is "no value". There is a distinct difference between "no value" and a value left blank. For instance, if a user submitted a form with foo=&bar=baz
, $_GET['foo']
is set to the value of empty string ""
, which is distinctly different from null
which would be the value for any key other than 'foo'
and 'bar'
.
That all being said, you can find out if a variable was never set or unset
, although they will always evaluate to true
with is_null
(is_null
is the negative of isset
with the exception that it will throw notices if the value was never set).
One way is if you have the variable in an array of some sort:
echo array_key_exists( $variableName, $theArray ) ? 'variable was set, possibly to null' : 'variable was never set';
If you need to check a global variable, use the $GLOBALS
array:
echo array_key_exists( $variableName, $GLOBALS ) ? 'variable exists in global scope' : 'this global variable doesn\'t exist';
The alternative method I've come up with for figuring out whether the variable was set is a bit more involved, and really unnecessary unless this is a feature that you absolutely have to have (in which case you should be able to build it without too much difficulty).
It relies on the fact that is_null
triggers a notice when a variable hasn't been set. Add an error handler that converts errors
into Exceptions
, and use a try...catch...
block to catch the exception that's thrown and set a flag in the catch
statement. Just after the catch
block execute your code that relies on this feature.
It's a dirty-nasty-hack if you ask me, and completely unnecessary, as null
should be considered the same as an unset
variable.