Is there a difference between isset
and !empty
. If I do this double boolean check, is it correct this way or redundant? and is there a shorter way to do the same thing?
isset($vars[1]) AND !empty($vars[1])
Is there a difference between isset
and !empty
. If I do this double boolean check, is it correct this way or redundant? and is there a shorter way to do the same thing?
isset($vars[1]) AND !empty($vars[1])
This is completely redundant.
empty
is more or less shorthand for!isset($foo) || !$foo
, and!empty
is analogous toisset($foo) && $foo
. I.e.empty
does the reverse thing ofisset
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:
You can simply use
!empty($vars[1])
here.Empty just check is the refered variable/array has an value if you check the php doc(empty) you'll see this things are considered emtpy
while isset check if the variable isset and not null which can also be found in the php doc(isset)
It is not necessary.
php.net
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.
empty()
function:Returns
FALSE
ifvar
has a non-empty and non-zero value.That’s a good thing to know. In other words, everything from
NULL
, to0
to “” will returnTRUE
when using theempty()
function.isset()
function returns:Returns
TRUE
ifvar
exists;FALSE
otherwise.In other words, only variables that don’t exist (or, variables with strictly
NULL
values) will returnFALSE
on theisset()
function. All variables that have any type of value, whether it is0
, a blank text string, etc. will returnTRUE
.