区分NULL和PHP中未定义

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

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.

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.