I also get confused how to check if a variable is false
/null
when returned from a function.
When to use empty()
and when to use isset()
to check the condition ?
For returns from functions, you use neither isset
nor empty
, since those only work on variables and are simply there to test for possibly non-existing variables without triggering errors.
For function returns checking for the existence of variables is pointless, so just do:
if (!my_function()) {
// function returned a falsey value
}
To read about this in more detail, see The Definitive Guide To PHP's isset
And empty
.