Check if value isset and null

2019-01-07 09:39发布

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:

$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.

I need to find a condition that satisfies the following:

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

Any ideas?

标签: php null isset
8条回答
老娘就宠你
2楼-- · 2019-01-07 10:05

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

查看更多
再贱就再见
3楼-- · 2019-01-07 10:05

Here some silly workaround using 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
查看更多
男人必须洒脱
4楼-- · 2019-01-07 10:15

If you are dealing with object properties whcih might have a value of NULL you can use: property_exists() instead of 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.

查看更多
Juvenile、少年°
5楼-- · 2019-01-07 10:15

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

 if( array_key_exists('foo', $GLOBALS) && is_null($foo)) // true & true => true
 if( array_key_exists('bar', $GLOBALS) && is_null($bar)) // false &  => false
查看更多
看我几分像从前
6楼-- · 2019-01-07 10:23

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

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

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

$bar = null;
if(isset($bar) && is_null($bar)) // returns true
查看更多
一夜七次
7楼-- · 2019-01-07 10:26

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')):

$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
查看更多
登录 后发表回答