php 检查值是否设置为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3803282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:10:21  来源:igfitidea点击:

Check if value isset and null

phpnullisset

提问by Tatu Ulmanen

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

我需要检查值是否定义为任何内容,包括空值。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 $baris undefined.

请注意,这$bar是未定义的。

I need to find a condition that satisfies the following:

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

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

Any ideas?

有任何想法吗?

采纳答案by Henrik Opel

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

回答by John Magnolia

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

如果您正在处理可能具有 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() 不同,即使该属性的值为 NULL,property_exists() 也会返回 TRUE。

回答by Lo?c Février

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

请参阅在 PHP 中测试变量是否存在的最佳方法;isset() 明显坏了

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

回答by nzn

I have found that compactis 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 = 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

Update

更新

As of PHP 7.3 compact()will give a notice for unset values, so unfortunately this alternative is no longer valid.

从 PHP 7.3 开始,compact()将给出未设置值的通知,因此不幸的是,此替代方案不再有效。

compact() now issues an E_NOTICE level error if a given string refers to an unset variable. Formerly, such strings have been silently skipped.

如果给定的字符串引用未设置的变量,compact() 现在会发出 E_NOTICE 级别的错误。以前,此类字符串已被悄悄跳过。

回答by masakielastic

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

}

回答by Raveline

You could use is_nulland emptyinstead of isset(). Empty doesn't print an error message if the variable doesn't exist.

您可以使用is_nullempty而不是 isset()。如果变量不存在,Empty 不会打印错误消息。

回答by Philippe Gerber

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

回答by Ruel

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 $baris defined and will only return true if:

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

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