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
Related videos on Youtube
![openfrog](https://i.stack.imgur.com/JtbrH.jpg?s=256&g=1)
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, 2022Comments
-
openfrog about 2 years
I mean... I "set" it to
NULL
. Soisset($somethingNULL) == true
?-
Flatlin3 over 14 yearswhy haven't you tried it yourself?
-
user229044 over 14 yearsWould 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 over 14 yearsnow you know you have to search php.net/manual for php reference documentation related questions :)
-
openfrog over 14 yearsI just love you all, that's why I come here for every reason I can find ;)
-
user985366 over 6 yearsIt 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 over 5 years@Flatlin3 because it can then help others that have the same question.
-
reformed almost 5 years@Flatlin3 what's wrong with documenting the answer?
-
-
Gregory Pakosz over 14 yearsRather 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 over 9 yearsSo how can I check for array key existence? Prior to finding this question, I thought
isset
returnstrue
fornull
variables/keys. -
Rob almost 7 years@TomášZato Use array_key_exists() instead.
-
Coanda over 4 yearsA similar function exists for objects: property_exists()