开发者

Test for NULL value in a variable that may not be set

Considering that:

  • The isset() construct returns TRUE if a variable is set and not NULL
  • The is_null() function throws a warning if the variable is not set

Is there a way to test whether a variable exists, no matter it's NULL or not, without using the @ operator to suppress the notice?


EDIT

Together with your first replies, I've been thinking about this and I'm getting the conclusion that inspecting get_defined_vars() is the only way to distinguish between a variable set to NULL and an unset variable开发者_Python百科. PHP seems to make little distinctions:

<?php

$exists_and_is_null = NULL;

// All these are TRUE

@var_dump(is_null($exists_and_is_null));
@var_dump(is_null($does_not_exist));

@var_dump($exists_and_is_null===NULL);
@var_dump($does_not_exist===NULL);

@var_dump(gettype($exists_and_is_null)=='NULL');
@var_dump(gettype($does_not_exist)=='NULL');

?>


$result = array_key_exists('varname', get_defined_vars());


As you already found out, you cannot :

  • rely on isset, as it return false for a variable that's null.
  • use $not_exists===null, as it'll raise a notice.


But you could be able to use a combinaison of :

  • get_defined_vars to get the list of existing variables, including those which are null,
  • and array_key_exists to find out if an entry exists in that list.


For instance :

$exists_and_null = null;
$exists_and_not_null = 10;

$defined_vars = get_defined_vars();

// true
var_dump(array_key_exists('exists_and_null', $defined_vars) 
    && $defined_vars['exists_and_null']===null);

// false
var_dump(array_key_exists('exists_and_not_null', $defined_vars) 
    && $defined_vars['exists_and_not_null']===null);

// false
var_dump(array_key_exists('not_exists', $defined_vars) 
    && $defined_vars['not_exists']===null);


A couple of notes :

  • In the first case, the variable exists => there is an entry in the list returned by get_defined_vars, so the second part of the condition is evaluated
    • and both parts of the condition are true
  • In the second case, the variable exists too, but is null
    • which means the first part of the condition is true, but the second one is false,
    • so the whole expression is false.
  • In the third case, the variable doesn't exist,
    • which means the first part of the condition is false,
    • and the second part of the condition is not evaluated -- which means it doesn't raise a notice.

But note this is probably not that a good idea, if you care about performances : isset is a language construct, and is fast -- while calling get_defined_vars is probably much slower ^^


I would argue here that any code requiring such a comparison would have gotten its semantics wrong; NULL is an unset value in a language that has no straightforward way of distinguishing between the two.


I used a self created function to check this easily, keep in mind it will fire off a PHP warning (I only monitor E_ERROR when I develop).

    function isNullOrEmpty( $arg )
    {
        if ( !is_array( $arg ) )
        {
            $arg = array( $arg );
        }

        foreach ( $arg as $key => $value )
        {
            if( $value == null || trim($value) == "" )
            {
                return true;
            }
        }
        return false;
    }


if (isset($var) && (is_null($var)) {
    print "\$var is null";
}

This should do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜