PHP检查False还是Null [英] PHP check if False or Null
问题描述
当从函数返回时,如何检查变量是否为false
/null
也让我感到困惑.
I also get confused how to check if a variable is false
/null
when returned from a function.
何时使用empty()
和何时使用isset()
检查条件?
When to use empty()
and when to use isset()
to check the condition ?
推荐答案
对于函数的返回,请不要使用isset
或empty
,因为它们仅对变量起作用,并且仅在此处测试可能不存在的变量变量而不会触发错误.
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
}
要详细了解此内容,请参见 PHP isset
和empty
.
To read about this in more detail, see The Definitive Guide To PHP's isset
And empty
.
这篇关于PHP检查False还是Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!