There are slight differences between testing a variable for its boolean value and checking whether it has been set. There might be circumstances where checking a variables boolean value is not going to give you the correct answer.
For example:
PHP Code:
$milkInYourCoffee = false;
if ( ! $milkinYourCoffee ) {
// The conditions will be satisfied and the if condition will be entered.
// However it erroneous to conclude that $milkInYourCoffee has not been set
// however it evaluates to false because that is the value it currently holds
}
$lumpsOfSugar = 0;
if (! $lumpsOfSugar ) {
// again it would be erroneous to conclude that $lumpsOfSugar has not been set
// and again it evaluates to false because 0 evaluates to false
}
Quite often when we are validating data - especially when it is user data then testing whether a variable is holding a 0, '' (empty string) or NULL value "(which all evaluate to false) is an appropriate test. Technically, however, it is not the same as using isset()
From the above code had I called isset() I would have got a different result
PHP Code:
$milkInYourCoffee = false;
$result = isset($milkInYourCoffee); // $result will be assigned TRUE
$lumpsOfSugar = 0;
$result = isset($lumpsOfSugar); // $result will be assigned TRUE
To complicate things a little further, one thing that isset() will return false on is if a variable has been set but assigned a value of NULL. If you need to test whether a variable exists but its value is currently NULL then you need to use is_null()
Clear as mud?
Bookmarks