Learning PHP: ISSET() vs. IS_NULL()

I found this little gem while looking into how to determine is a variable is usable; in my case, if it’s been set:


claude dot pache at gmail dot com
09-May-2009 04:38
A small but important difference between “is_null” and “isset” is the following: “is_null” tests if an *expression* (not a *variable*) is null, while “isset” tests if a *variable* has null value or is undefined. The difference is manifested in the two following experiments:

Experiment 1:

function test() { return null; }
var_dump(is_null(test())); // displays "true"
var_dump(isset(test())); // parse error, because "test()" is not a variable

Experiment 2:

var_dump(isset($an_undefined_variable)); // displays "false" since "$an_undefined_variable" is not defined
var_dump(is_null($an_undefined_variable)); // displays "true" (as expected), but throws a notice because "$an_undefined_variable" is not defined.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.