107

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?

4
  • 27
    if(isset($foo)) // returns false, i fell off the chair, all these years...
    – max4ever
    Sep 21, 2012 at 13:42
  • in_array($key,array_keys($_SESSION)) && is_null($_SESSION[$key]) I was wondering this for so long ..
    – Jack
    Nov 28, 2018 at 7:57
  • 3
    This is not a normal behave for me, isset = is set ?, your variable is set at null. I wasted lot of time because of this one... Aug 13, 2019 at 11:09
  • 1
    @VincentDecaux it's PHP... Just an example of many more illogical things in the language...
    – Sebi2020
    Sep 15, 2021 at 12:56

11 Answers 11

99

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
4
  • +1 I was going to suggest the same function, get_defined_vars happily copes with scope.
    – salathe
    Sep 27, 2010 at 12:00
  • 1
    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. Sep 27, 2010 at 12:13
  • 5
    well, you don't need vars, so in theory its one line "if(array_key_exists('foo',get_defined_vars())){} "
    – Hannes
    Sep 27, 2010 at 12:47
  • fvn's newer answer might be a quicker way to get a variable that exists in current context, avoiding the cost of get_defined_vars(): array_key_exists('foo', compact('foo')). Or faster, if testing a global: array_key_exists('foo', $GLOBALS). Jan 9, 2017 at 22:41
33

If you are dealing with object properties which 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.

3
  • 14
    You can do the same for arrays with array_key_exists();
    – Teaqu
    Feb 26, 2014 at 15:40
  • Yes, but it is NOT possible to use this one to see if a declared property has yet been initialised. For this you can use isset(), but then of course you have a problem with readonly properties if they have been initialised to null. I've seen people grabbing to Refelction in those cases, another solution is Try/Catch but it shouldn't be that complicated!
    – Roemer
    Jul 25 at 13:56
  • Plus the fact that dynamic properties are deprecated nowadays make this method (after 9 years of being useful) not recommended.
    – Roemer
    Jul 25 at 14:00
16

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
4
  • 3
    The code you quote only works if the variable is in the global scope.
    – Raveline
    Sep 27, 2010 at 11:33
  • 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'. Sep 27, 2010 at 11:37
  • Using $GLOBALS seems a bit volatile, I have to do some testing myself before I can declare this as working. Sep 27, 2010 at 12:14
  • Just a note 13 years later: do not use global variables. We've moved on, it never was a good idea to begin with.
    – Roemer
    Jul 25 at 13:57
8

I found this topic when I was looking for a solution for an array. to check for the presence of an array element that contains NULL, this construction helped me

    $arr= [];
    $foo = 'foo';
    $arr[$foo]= NULL;
    if (array_key_exists('bar', $arr)) {}; // Should evaluate to FALSE
    if (array_key_exists('foo', $arr)) {}; // Should evaluate to TRUE
    if (array_key_exists($foo, $arr)) {}; // Should evaluate to TRUE
5

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

Update

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

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

2
  • Interesting alternative. But note that it is probably slower than calling array_key_exists on an existing array, such as $GLOBALS - because a look up in a hash table does not get any slower, when the table gets large, and you've added the extra work of compact. Nevertheless, I upvoted it because it is useful in one situation: if you want to know whether foo exists in the current context, regardless of where it came from - if you don't care whether is local or global, just want to know whether it exists. Jan 9, 2017 at 22:27
  • 1
    @ToolmakerSteve - I was actually referring to the potentially significant overhead of calling get_defined_vars. See here.
    – nzn
    Jan 10, 2017 at 13:42
1

The following code written as PHP extension is equivalent to array_key_exists($name, get_defined_vars()) (thanks to Henrik and 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;
    }

}
0

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
1
  • 1
    Doesn't look very portable.. :) Sep 27, 2010 at 12:09
0

In my case I had the following code:

class SomeClass {

  private $cachedInstance;

  public instance()
  {
    if (! isset($this->cachedInstance)) {
      $this->cachedInstance = GetCachedInstanceFromDb(); // long operation, that could return Null if the record not found
    }

    return $this->cachedInstance;
  }
}

And it failed in a way that GetCachedInstanceFromDb() got called multiple times if it returned null. All because isset() would return false even if the property was explicitly set to Null.

So, I had to do the following changes:

  1. Declare the property with initial value set to False;

  2. Use strict (type-safe) comparison when checking for the current variable value;

class SomeClass {

  private $cachedInstance = false; // #1

  public instance()
  {
    if ($this->cachedInstance === false) { // #2
      $this->cachedInstance = GetCachedInstanceFromDb();
    }

    return $this->cachedInstance;
  }
}
-1

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

4
  • I am using is_null. The result is same regardless of the isset. Sep 27, 2010 at 11:27
  • I made a mistake while posting my first answer : did you try with empty() ?
    – Raveline
    Sep 27, 2010 at 11:32
  • 2
    This won't work for values that are not empty and not NULL such as FALSE, 0, array() or "".
    – Teaqu
    Feb 26, 2014 at 15:22
  • 1
    This answer is wrong. is_null has the same problem as is_set: it can't distinguish between "not set" and "set to null", which is the problem OP has. empty is even worse, as Calum points out. Jan 9, 2017 at 22:17
-1

At risk of being downvoted, I wouldn't even bother - clearly PHP wanted you to logically think of NULL and Undef as the same. I just ran with it - I created a function:

bool isEmpty(& $davar);

that checks for isset (handles both null and undef), "", and array(). Note that this is purposefully not dealing with falseness; just empty. The & 'reference-izer' allows the variable to be passed even though undefined without an error message, and if you check for isset and return false first, your next checks against "" and array() can be made without error.

The next function takes advantage of this function and is used where you would use

$davar || some-default.

and that is:

mixed defaultForEmpty(& $daVar, $default);

which just has the condition:

if (isEmpty($daVar)) 
    return $default;
else
    return $daVar;

BTW, these work with object references, array indexes, $_GET, $_POST, etc..

-3

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
4
  • No, he said that if(isset($bar)) gives false when $bar = null. Sep 27, 2010 at 11:32
  • 2
    This will not pass any other variables than null (eg. if $bar = "test"). Sep 27, 2010 at 12:08
  • 3
    When $bar = null isset() will return "false" and is_null() will return true. False and true gives always false. Jul 20, 2013 at 21:39
  • This answer is completely wrong. As OP said, isset($bar) returns false, even after $bar = null;. Jan 9, 2017 at 22:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.