为什么要同时检查isset()和!empty() [英] Why check both isset() and !empty()

查看:104
本文介绍了为什么要同时检查isset()和!empty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

isset!empty之间是否存在差异.如果我进行双重布尔检查,这种方法是正确的还是多余的?而且有没有更短的方法来做同样的事情?

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])

推荐答案

这是完全多余的. empty或多或少是!isset($foo) || !$foo的简写,并且!emptyisset($foo) && $foo类似. IE. empty执行与isset相反的操作,并另外检查值的真实性.

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.

换句话说,empty!$foo相同,但是如果变量不存在,则不会发出警告.这就是该功能的重点:进行布尔比较,而不必担心会设置变量.

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.

手册内容如下:

empty()(boolean) var相反,不同之处在于未设置变量时不生成警告.

您可以在这里简单地使用!empty($vars[1]).

You can simply use !empty($vars[1]) here.

这篇关于为什么要同时检查isset()和!empty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆