In PHP, usually variables that have not been set or have been canceled are considered null . The null value is "no value". There is a great difference between βno valueβ and a value left blank. For example, if the user submitted a form with foo=&bar=baz , $_GET['foo'] set to the empty string "" , which is clearly different from null , which will be the value for any key except 'foo' and 'bar' .
To get it all said, you can find out if a variable or unset , although they will always be evaluated as true with is_null ( is_null is a negative isset value with the exception that it will notify if the value has never been set).
One way is if you have a variable in an array of some type:
echo array_key_exists( $variableName, $theArray ) ? 'variable was set, possibly to null' : 'variable was never set';
If you need to check a global variable, use the $GLOBALS array:
echo array_key_exists( $variableName, $GLOBALS ) ? 'variable exists in global scope' : 'this global variable doesn\'t exist';
The alternative method that I came up with to determine whether the variable was set is a little more involved and really unnecessary, unless it means what you need to have (in which you should be able to build it without much difficulty).
It relies on the fact that is_null triggers a notification when a variable has not been set. Add an error handler that converts errors to Exceptions , and use the try...catch... to catch the exception that threw and set the flag in the catch statement. Once the catch executes your code that will use this function.
This is a dirty nasty hack if you ask me and are completely unnecessary, since null should be considered as an unset variable.
source share