1

Simple question: In php, is var set to null same as undefined?

In addition, what is simplest way to check is var null?

How to use this functionality without breaking code, eg. by setting something optional in config array that can be set to null.

If check for null with is_null it can potentially break code if var is not defined.

is_null is completely opposite of isset, but can used only and only if you are certain that var is defined. Fore every other cases looks that isset more appropriate.

And to check null, isset is unapropriate, is it?

So var can be:

  • undefined
  • null
  • empty
  • with content to check with is_* functions (numeric, string, array,...)
Vladimir Vukanac
  • 944
  • 16
  • 29
  • Please rephrase your question, as the question title asks if they are the same, which I answered, but wasn't what you were really asking. – Mike Nov 11 '14 at 16:18
  • But you stated that it is the same. Or I am wrong? – Vladimir Vukanac Nov 11 '14 at 16:26
  • The two states are exactly equal, yes, but that is not exactly what you are asking. You are asking if you can check for the difference in an array, which, luckily, you can. – Mike Nov 11 '14 at 16:28
  • not just in array, check $var as is, not array. But, thank you for contribution. We found same answer. – Vladimir Vukanac Nov 11 '14 at 16:43
  • Can not be done for straight variables without the use of `get_defined_vars()` which is really only practical as a proof, and not in production. – Mike Nov 11 '14 at 18:16
  • Yeah, I noticed we found the same answer, at about the same rate. Curious, that... – Mike Nov 11 '14 at 18:16

3 Answers3

1

var set to NULL is not the same as undefined.

<?php
if (is_null($undefinedvariable)) {
    echo 'This variable is NULL';
}

This code demonstrates how confusion may arise, as it will result in the message 'This variable is NULL' being displayed, however it will also generate an "Undefined variable" notice. (assuming $undefinedvariable is actually undefined of course!)

user1187347
  • 318
  • 3
  • 12
0

In this page it looks like it is NOT same but acts like it is.

To check:

  • undefined: !(isset($var) || array_key_exists('var',get_defined_vars())))
  • null: array_key_exists('var', get_defined_vars()) && is_null($var)
  • empty: isset($var) && empty($var)
  • content: isset($var) && !empty($var) + any other if_* specific.

To make same check with my config array instead get_defined_vars() I simply used $configArr, and it can be used any other array like filtered POST.

To check null:

$configArr = array('var' => null);

$isNull = array_key_exists('var', $configArr) && is_null($configArr['var']);

Examples

Array
(
    [str] => Array
        (
            [isset] => TRUE
            [is_null] => FALSE
            [empty] => FALSE
            [array_key_exist] => TRUE
            [array_key_exist + is_null] => FALSE
        )

    [empty] => Array
        (
            [isset] => TRUE
            [is_null] => FALSE
            [empty] => TRUE
            [array_key_exist] => TRUE
            [array_key_exist + is_null] => FALSE
        )

    [null] => Array
        (
            [isset] => FALSE
            [is_null] => TRUE
            [empty] => TRUE
            [array_key_exist] => TRUE
            [array_key_exist + is_null] => TRUE
        )

    [undefined] => Array
        (
            [isset] => FALSE
            [is_null] => TRUE
            [empty] => TRUE
            [array_key_exist] => FALSE
            [array_key_exist + is_null] => FALSE
        )

)

Final result:

$pRes = (!array_key_exists('var', $arrayOfVars)
            ? 0 // undefined
            : (is_null($arrayOfVars['var'])
                ? 1     // -> null
                : (empty($arrayOfVars['var']
                    ? 2  // -> empty
                    : 3  // -> custom
                    )
                )
            );
Mike
  • 1,968
  • 18
  • 35
Vladimir Vukanac
  • 944
  • 16
  • 29
0

When questioning this I frequently refer to this website:
https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

It will provide you with a list of all of the equivalencies and what their truthiness is.

(undefined $var) == !isset($var) == empty($var) == is_null($var)

(var $var;)      == !isset($var) == empty($var) == is_null($var)

($var = NULL)    == !isset($var) == empty($var) == is_null($var)

So, yes, the two are equivalent.

The rest of your question gets complicated (in IDEOne):

<table><tr><td>Key</td><td>array_key_exists()</td><td>isset()</td><td>empty()</td><td>is_null()</td></tr><tr><td>$array['null']</td><td>true</td><td>false</td><td>true</td><td>true</td></tr><tr><td>$array['empty']</td><td>true</td><td>true</td><td>true</td><td>false</td></tr><tr><td>$array['zero']</td><td>true</td><td>true</td><td>true</td><td>false</td></tr><tr><td>$array['space']</td><td>true</td><td>true</td><td>false</td><td>false</td></tr><tr><td>$array['character']</td><td>true</td><td>true</td><td>false</td><td>false</td></tr><tr><td>$array['true']</td><td>true</td><td>true</td><td>false</td><td>false</td></tr><tr><td>$array['false']</td><td>true</td><td>true</td><td>true</td><td>false</td></tr><tr><td>$array['undefined']</td><td>false</td><td>false</td><td>true</td><td>true</td></tr></table>

If you want to do implicit checks, you have to do a combination:

if(array_key_exists($key, $array) {
    $check = $array[$key];
    if($check === NULL) {
        // set and null
    } elseif ($check === FALSE) {
        // set and FALSE
    } elseif ( /** additional checks, etc, etc... **/ ) { }
} else {
    // Not set
}

Unfortunately, if the variable is not in an array, this is not possible to do without 'get_defined_vars()`, and I would highly discourage it's use in production code, as it will eat up memory due to non-referenced data (Objects will be represented by reference, but this will duplicate all other variables, causing an sharp increase in memory).

It is good coding practice to simply consider that undefined == !isset == NULL == empty. Because what you are looking for is data, and no data is no data.

Mike
  • 1,968
  • 18
  • 35
  • Actually I did not like completely that url. I saw it earlier, so I had to dig little more. And yes, I don't want to be equivalent. Eg. If in config array I skip it has to be undefined, if I enter it with null it has to show that it is null, and that is different than undefined. – Vladimir Vukanac Nov 11 '14 at 16:12
  • Added some help for you. – Mike Nov 11 '14 at 16:17
  • @MrW - I also added the `array_key_exists()` check. – Mike Nov 11 '14 at 16:21