检查值是否为set和null

时间:2021-07-09 17:07:15

I need to check if value is defined as anything, including null. isset treats null values as undefined and returns false. Take the following as an example:

我需要检查值是否定义为任何值,包括null。isset将空值视为未定义,并返回false。以以下为例:

$foo = null;

if(isset($foo)) // returns false
if(isset($bar)) // returns false
if(isset($foo) || is_null($foo)) // returns true
if(isset($bar) || is_null($bar)) // returns true, raises a notice

Note that $bar is undefined.

注意$bar没有定义。

I need to find a condition that satisfies the following:

我需要找到一个满足以下条件的条件:

if(something($bar)) // returns false;
if(something($foo)) // returns true;

Any ideas?

什么好主意吗?

8 个解决方案

#1


66  

IIRC, you can use get_defined_vars() for this:

IIRC,可以使用get_defined_vars():

$foo = NULL;
$vars = get_defined_vars();
if (array_key_exists('bar', $vars)) {}; // Should evaluate to FALSE
if (array_key_exists('foo', $vars)) {}; // Should evaluate to TRUE

#2


22  

If you are dealing with object properties whcih might have a value of NULL you can use: property_exists() instead of isset()

如果您正在处理对象属性,那么whcih的值可能为NULL,您可以使用:property_exists()而不是isset()

<?php

class myClass {
    public $mine;
    private $xpto;
    static protected $test;

    function test() {
        var_dump(property_exists($this, 'xpto')); //true
    }
}

var_dump(property_exists('myClass', 'mine'));   //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto'));   //true, as of PHP 5.3.0
var_dump(property_exists('myClass', 'bar'));    //false
var_dump(property_exists('myClass', 'test'));   //true, as of PHP 5.3.0
myClass::test();

?>

As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

与isset()相反,property_exists()返回TRUE,即使属性的值为空。

#3


12  

See Best way to test for a variable's existence in PHP; isset() is clearly broken

查看在PHP中测试变量是否存在的最佳方法;收取()显然是坏了

 if( array_key_exists('foo', $GLOBALS) && is_null($foo)) // true & true => true
 if( array_key_exists('bar', $GLOBALS) && is_null($bar)) // false &  => false

#4


2  

I have found that compact is a function that ignores unset variables but does act on ones set to null, so when you have a large local symbol table I would imagine you can get a more efficient solution over checking array_key_exists('foo', get_defined_vars()) by using array_key_exists('foo', compact('foo')):

我发现compact是一个忽略未设置变量的函数,但是它对设置为null的变量起作用,所以当你有一个大的局部符号表时,我可以想象你可以通过检查array_key_exists('foo' get_defined_vars(),通过使用array_key_exists('foo' compact('foo' foo' foo')))得到一个更高效的解决方案:

$foo = null;
echo isset($foo) ? 'true' : 'false'; // false
echo array_key_exists('foo', compact('foo')) ? 'true' : 'false'; // true
echo isset($bar) ? 'true' : 'false'; // false
echo array_key_exists('bar', compact('bar')) ? 'true' : 'false'; // false

#5


1  

The following code written as PHP extension is equivalent to array_key_exists($name, get_defined_vars()) (thanks to Henrik and Hannes).

以下作为PHP扩展编写的代码等价于array_key_exists($name, get_defined_vars())(感谢Henrik和Hannes)。

// get_defined_vars()
// https://github.com/php/php-src/blob/master/Zend/zend_builtin_functions.c#L1777
// array_key_exists
// https://github.com/php/php-src/blob/master/ext/standard/array.c#L4393

PHP_FUNCTION(is_defined_var)
{

    char *name;
    int name_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
        return;
    }

    if (!EG(active_symbol_table)) {
        zend_rebuild_symbol_table(TSRMLS_C);
    }

    if (zend_symtable_exists(EG(active_symbol_table), name, name_len + 1)) {
        RETURN_TRUE;
    }

}

#6


0  

You could use is_null and empty instead of isset(). Empty doesn't print an error message if the variable doesn't exist.

您可以使用is_null和empty代替isset()。如果变量不存在,则Empty不打印错误消息。

#7


0  

Here some silly workaround using xdebug. ;-)

这里有一些使用xdebug的愚蠢方法。:-)

function is_declared($name) {
    ob_start();
    xdebug_debug_zval($name);
    $content = ob_get_clean();

    return !empty($content);
}

$foo = null;
var_dump(is_declared('foo')); // -> true

$bla = 'bla';
var_dump(is_declared('bla')); // -> true

var_dump(is_declared('bar')); // -> false

#8


-2  

is_null($bar) returns true, since it has no values at all. Alternatively, you can use:

is_null($bar)返回true,因为它根本没有任何值。或者,您可以使用:

if(isset($bar) && is_null($bar)) // returns false

to check if $bar is defined and will only return true if:

检查$bar是否已定义,并且只在以下情况下返回true:

$bar = null;
if(isset($bar) && is_null($bar)) // returns true

#1


66  

IIRC, you can use get_defined_vars() for this:

IIRC,可以使用get_defined_vars():

$foo = NULL;
$vars = get_defined_vars();
if (array_key_exists('bar', $vars)) {}; // Should evaluate to FALSE
if (array_key_exists('foo', $vars)) {}; // Should evaluate to TRUE

#2


22  

If you are dealing with object properties whcih might have a value of NULL you can use: property_exists() instead of isset()

如果您正在处理对象属性,那么whcih的值可能为NULL,您可以使用:property_exists()而不是isset()

<?php

class myClass {
    public $mine;
    private $xpto;
    static protected $test;

    function test() {
        var_dump(property_exists($this, 'xpto')); //true
    }
}

var_dump(property_exists('myClass', 'mine'));   //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto'));   //true, as of PHP 5.3.0
var_dump(property_exists('myClass', 'bar'));    //false
var_dump(property_exists('myClass', 'test'));   //true, as of PHP 5.3.0
myClass::test();

?>

As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

与isset()相反,property_exists()返回TRUE,即使属性的值为空。

#3


12  

See Best way to test for a variable's existence in PHP; isset() is clearly broken

查看在PHP中测试变量是否存在的最佳方法;收取()显然是坏了

 if( array_key_exists('foo', $GLOBALS) && is_null($foo)) // true & true => true
 if( array_key_exists('bar', $GLOBALS) && is_null($bar)) // false &  => false

#4


2  

I have found that compact is a function that ignores unset variables but does act on ones set to null, so when you have a large local symbol table I would imagine you can get a more efficient solution over checking array_key_exists('foo', get_defined_vars()) by using array_key_exists('foo', compact('foo')):

我发现compact是一个忽略未设置变量的函数,但是它对设置为null的变量起作用,所以当你有一个大的局部符号表时,我可以想象你可以通过检查array_key_exists('foo' get_defined_vars(),通过使用array_key_exists('foo' compact('foo' foo' foo')))得到一个更高效的解决方案:

$foo = null;
echo isset($foo) ? 'true' : 'false'; // false
echo array_key_exists('foo', compact('foo')) ? 'true' : 'false'; // true
echo isset($bar) ? 'true' : 'false'; // false
echo array_key_exists('bar', compact('bar')) ? 'true' : 'false'; // false

#5


1  

The following code written as PHP extension is equivalent to array_key_exists($name, get_defined_vars()) (thanks to Henrik and Hannes).

以下作为PHP扩展编写的代码等价于array_key_exists($name, get_defined_vars())(感谢Henrik和Hannes)。

// get_defined_vars()
// https://github.com/php/php-src/blob/master/Zend/zend_builtin_functions.c#L1777
// array_key_exists
// https://github.com/php/php-src/blob/master/ext/standard/array.c#L4393

PHP_FUNCTION(is_defined_var)
{

    char *name;
    int name_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
        return;
    }

    if (!EG(active_symbol_table)) {
        zend_rebuild_symbol_table(TSRMLS_C);
    }

    if (zend_symtable_exists(EG(active_symbol_table), name, name_len + 1)) {
        RETURN_TRUE;
    }

}

#6


0  

You could use is_null and empty instead of isset(). Empty doesn't print an error message if the variable doesn't exist.

您可以使用is_null和empty代替isset()。如果变量不存在,则Empty不打印错误消息。

#7


0  

Here some silly workaround using xdebug. ;-)

这里有一些使用xdebug的愚蠢方法。:-)

function is_declared($name) {
    ob_start();
    xdebug_debug_zval($name);
    $content = ob_get_clean();

    return !empty($content);
}

$foo = null;
var_dump(is_declared('foo')); // -> true

$bla = 'bla';
var_dump(is_declared('bla')); // -> true

var_dump(is_declared('bar')); // -> false

#8


-2  

is_null($bar) returns true, since it has no values at all. Alternatively, you can use:

is_null($bar)返回true,因为它根本没有任何值。或者,您可以使用:

if(isset($bar) && is_null($bar)) // returns false

to check if $bar is defined and will only return true if:

检查$bar是否已定义,并且只在以下情况下返回true:

$bar = null;
if(isset($bar) && is_null($bar)) // returns true