Is var set to null same as undefined and how to check differences

孤街醉人 提交于 2019-12-11 09:31:26

问题


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,...)

回答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!)




回答2:


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.




回答3:


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
                    )
                )
            );


来源:https://stackoverflow.com/questions/26868652/is-var-set-to-null-same-as-undefined-and-how-to-check-differences

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!