-1

is there any way to differentiate between a variable not having been defined yet or had been defined but is set to 'NULL'

2

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

in my particular case property_exists saved the day ... but for normal operations get_defined_vars() as mentioned by @hades2510 seems more appropiate

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.