tags:

views:

134

answers:

5

Hi,

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?

+2  A: 

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

Raveline
I am using is_null. The result is same regardless of the `isset`.
Tatu Ulmanen
I made a mistake while posting my first answer : did you try with empty() ?
Raveline
+3  A: 

See http://stackoverflow.com/questions/418066/best-way-to-test-for-a-variables-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
Loïc Février
The code you quote only works if the variable is in the global scope.
Raveline
Indeed but isn't it the most frequent case ? In a function you will have variables at global scope and arguments (which are always defined). You could also have object properties but then you can use 'property_exists'.
Loïc Février
Using $GLOBALS seems a bit volatile, I have to do some testing myself before I can declare this as working.
Tatu Ulmanen
A: 

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
Ruel
No, he said that `if(isset($bar))` gives false when `$bar = null`.
Loïc Février
my bad. will update my answer.
Ruel
This will not pass any other variables than null (eg. if `$bar = "test"`).
Tatu Ulmanen
A: 

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
Philippe Gerber
Doesn't look very portable.. :)
Tatu Ulmanen
Indeed. And performance may drop as well. ;-)
Philippe Gerber
+4  A: 

IIRC, you can use get_defined_vars() for this:

$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
Henrik Opel
+1 I was going to suggest the same function, `get_defined_vars` happily copes with scope.
salathe
Seems to be working, but I was hoping for something simpler. Oh well. Let's see if anyone can come up with a one liner.
Tatu Ulmanen
well, you don't need vars, so in theory its one line "if(array_key_exists('foo',get_defined_vars())){} "
Hannes