Is it just me or are we missing a way in the language to check if a
variable that has been set to NULL
exists or not?
is_null()
on an unset variable throws a NOTICE.
$var === null throws a notice.
So, you have to use isset()? But, ah,
$var = null;
if(isset($var))
yields false.
Is array_key_exists("var", $GLOBALS) the only solution to this problem?
Seems silly.
perhaps an var_exists() function is needed to fill this hole?
--
Brian.
Is it just me or are we missing a way in the language to check if a variable
that has been set toNULL
exists or not?
is_null()
on an unset variable throws a NOTICE.
$var === null throws a notice.So, you have to use isset()? But, ah,
$var = null;
if(isset($var))yields false.
Is array_key_exists("var", $GLOBALS) the only solution to this problem?
This question creeps up every once in a while, and I have seen
bunchload of code that uses array_key_exists("foo", $array); rather
then isset($array["foo"]) because of it.
I however have never seen a proper usecase..
I tend to "initialize" my variables, and array keys, as null, so I
never have to do these kind of checks. And if you don't initialize
your variables, then you can turn of E_NOTICE
if it bothers you.
-Hannes
Is it just me or are we missing a way in the language to check if a
variable that has been set toNULL
exists or not?
...
perhaps an var_exists() function is needed to fill this hole?
php -r 'function f(){$f=null; var_dump(array_key_exists("f", get_defined_vars()
));} f();'
Mike
php -r 'function f(){$f=null; var_dump(array_key_exists("f",
get_defined_vars()
));} f();'
So, two function calls? That is not a solution to simply check if a
variable has been created. You are creating an array and operating on
said array. It should be a language construct. Why use an array function
to decide something about a non-array variable?