Why check both isset() and !empty()
Solution 1:
This is completely redundant. empty
is more or less shorthand for !isset($foo) || !$foo
, and !empty
is analogous to isset($foo) && $foo
. I.e. empty
does the reverse thing of isset
plus an additional check for the truthiness of a value.
Or in other words, empty
is the same as !$foo
, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.
The manual puts it like this:
empty()
is the opposite of(boolean) var
, except that no warning is generated when the variable is not set.
You can simply use !empty($vars[1])
here.
Solution 2:
isset()
tests if a variable is set and not null:
http://us.php.net/manual/en/function.isset.php
empty()
can return true when the variable is set to certain values:
http://us.php.net/manual/en/function.empty.php
To demonstrate this, try the following code with $the_var unassigned, set to 0, and set to 1.
<?php
#$the_var = 0;
if (isset($the_var)) {
echo "set";
} else {
echo "not set";
}
echo "\n";
if (empty($the_var)) {
echo "empty";
} else {
echo "not empty";
}
?>