0

I need to differenciate a completely undefined variable from a defined variable containing null. Here is what I tried so far :

$v1 = null;
$variables = get_defined_vars();

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 (isset($variables['v1']) ? '$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 (isset($variables['v2']) ? '$v2 defined' : '$v2 not defined') . PHP_EOL;

This outputs :

$v1 not set
$v1 null
$v1 empty
$v1 not defined

$v2 not set
$v2 null
$v2 empty
$v2 not defined

You may run this example here

I get the exact same results with both variables, but PHP emits a notice about calling is_null on the undefined $v2 variable. So PHP does indeed make a difference between undefined and null. What test could I run on the variables to see this difference ?

UPDATE 1 : based on this question, the only way to do this is to detect the notice PHP emits, which is extremly dirty.

UPDATE 2 : Actually, it can be done, I was using get_defined_vars() wrong, somehow

$v1 = null;
echo (array_key_exists('v1', get_defined_vars()) ? '$v1 defined' : '$v1 not defined') . PHP_EOL;
echo (array_key_exists('v2', get_defined_vars()) ? '$v2 defined' : '$v2 not defined') . PHP_EOL;

This outputs :

$v1 defined
$v2 not defined

Thank you Iłya Bursov from pointing this out

Thibault Witzig
  • 2,060
  • 2
  • 19
  • 30
  • just wondering - why do you need that? I cannot imagine scenario where it will matter... – Iłya Bursov Mar 24 '20 at 13:36
  • `gettype($v1) !== null` ? – 0stone0 Mar 24 '20 at 13:37
  • See also https://stackoverflow.com/questions/17398816/what-is-the-php-equivalent-of-javascripts-undefined – Patrick Q Mar 24 '20 at 13:37
  • 1
    @Somangshu Undefined variables don't equal the string 'undefined'! Comparison to `null` doesn't allow you to *differentiate* between `null` and undefined. – deceze Mar 24 '20 at 13:37
  • 1
    @0stone0 Raises a notice if the variable is undefined! – deceze Mar 24 '20 at 13:38
  • 2
    @0stone0 You cannot distinguish between an undefined variable and a variable containing `null` using `gettype` and `isset`. – deceze Mar 24 '20 at 13:39
  • I get these two situations (a variable that is either undefined or null) as possible results of the same poorly designed old piece of code. Both results actually have very different meanings. My goal here is to determine if I can somehow deal with this result as it is or if I must get my hands dirty and improve the old code. Apparently, the only way to diffrenciate null and undefined is some dirty magic with PHP error output. I guess I know what I have to do... :) – Thibault Witzig Mar 24 '20 at 13:47
  • 2
    BTW `array_key_exists('v1', get_defined_vars())` returns true – Iłya Bursov Mar 24 '20 at 14:04

0 Answers0