Will isset() return false if I assign NULL to a variable?

19,927

Solution 1

bool isset ( mixed $var [, mixed $var [, $... ]] )

Determine if a variable is set and is not NULL.

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

Return values

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

From the manual. Examples on the same page.

Solution 2

Yes - from the ISSET() documentation:

$foo = NULL;
var_dump(isset($foo));   // FALSE

/* Array example */
$a = array ('test' => 1, 'hello' => NULL);

var_dump(isset($a['test']));            // TRUE
var_dump(isset($a['foo']));             // FALSE
var_dump(isset($a['hello']));           // FALSE
Share:
19,927

Related videos on Youtube

openfrog
Author by

openfrog

Thanks a lot to Pascal MARTIN, Gumbo and Quassnoi for their great help. Also many thanks to everyone else answering my questions.

Updated on April 29, 2022

Comments

  • openfrog
    openfrog about 2 years

    I mean... I "set" it to NULL. So isset($somethingNULL) == true?

    • Flatlin3
      Flatlin3 over 14 years
      why haven't you tried it yourself?
    • user229044
      user229044 over 14 years
      Would have taken less time to test than to ask the question. You even typed the code needed to test your question into the question itself.
    • Gregory Pakosz
      Gregory Pakosz over 14 years
      now you know you have to search php.net/manual for php reference documentation related questions :)
    • openfrog
      openfrog over 14 years
      I just love you all, that's why I come here for every reason I can find ;)
    • user985366
      user985366 over 6 years
      It took me less time to find this question and answer than it would have taken to figure out test cases, write code, and run it, and still not be sure if I had covered all cases.
    • htafoya
      htafoya over 5 years
      @Flatlin3 because it can then help others that have the same question.
    • reformed
      reformed almost 5 years
      @Flatlin3 what's wrong with documenting the answer?
  • Gregory Pakosz
    Gregory Pakosz over 14 years
    Rather copy pasting the doc as I did :D But we're doing it to avoid just putting a link in case it gives a 404 afterwards (unlikely to happen with php.net though)
  • Tomáš Zato
    Tomáš Zato over 9 years
    So how can I check for array key existence? Prior to finding this question, I thought isset returns true for null variables/keys.
  • Rob
    Rob almost 7 years
    @TomášZato Use array_key_exists() instead.
  • Coanda
    Coanda over 4 years
    A similar function exists for objects: property_exists()