Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell whether a variable is null or undefined in php

Tags:

php

Is there a single function that can be created to tell whether a variable is NULL or undefined in PHP? I will pass the variable to the function (by reference if needed) but I won't know the name of the variable until runtime.

isset() and is_null() do not distinguish between NULL and undefined.
array_key_exists requires you to know the name of the variable as you're writing your code.
And I haven't found a way to determine the name of a variable without defining it.

Edit

I've also realized that passing a variable by reference automatically defines it.

Elaboration

Through the collection of these answers and comments I've determined that the short answer to my question is "No". Thank you for all the input.

Here are some details on why I needed this:

I've created a PHP function called LoadQuery() that pulls a particular SQL query from an array of queries and then prepares it for submission to MySQL. Most-importantly I scan the query for variables (like $UserID) that I then replace with their values from the current scope. In creating this function I needed a way to determine if a variable had been declared, and was NULL, empty, or had a value. This is why I may not know the name of the given variable until runtime.

like image 898
Andrew Avatar asked Aug 01 '11 23:08

Andrew


2 Answers

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

like image 117
Iłya Bursov Avatar answered Nov 15 '22 14:11

Iłya Bursov


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
}
like image 21
Phil Avatar answered Nov 15 '22 16:11

Phil