is there any way to differentiate between a variable not having been defined yet or had been defined but is set to 'NULL'
-
Duplicate of Check if variable is_undefined in PHP stackoverflow.com/questions/16993027/…faintsignal– faintsignal2014-03-22 20:49:28 +00:00Commented Mar 22, 2014 at 20:49
-
Possible duplicate of Check if variable is_undefined in PHPfaintsignal– faintsignal2018-01-01 00:05:03 +00:00Commented Jan 1, 2018 at 0:05
2 Answers
Probably not.
You can read about NULL here.
After a little more digging it may be possible to use get_defined_vars() and check for the variable name as a key in the returned array.
This works even if the var was assigned NULL.
Comments
You can check $GLOBALS, if it contains certain key. Does not work for variables defined inside functions:
array_key_exists('variable name', $GLOBALS);
For objects, check *property_exists* function.
Although, I'd suggest that you avoid creating any code that relies on (in)existence of any variable. If you use a var, it should be defined from start of the script/object instantiation. If you have to carry more information, that you can hold in value/null, you should not go for value/null/unset way, you might want to create another boolean var, etc. And I recommend creating a code that won't issue any E_NOTICE because of operations on inexistent variables.