加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – !empty($x)和@ $x之间的区别?

发布时间:2020-12-13 17:25:28 所属栏目:PHP教程 来源:网络整理
导读:if(!empty($x))和if(@ $x)之间是否有任何功能差异? 注:我知道@抑制错误,我不会轻易使用它. 解决方法 可能没有其他人指出的功能差异,但使用if(@ $x)似乎不正确并且有理由不使用它. 从关于suppression error operator的文档: If you have set a custom err
if(!empty($x))和if(@ $x)之间是否有任何功能差异?

注:我知道@抑制错误,我不会轻易使用它.

解决方法

可能没有其他人指出的功能差异,但使用if(@ $x)似乎不正确并且有理由不使用它.

从关于suppression error operator的文档:

If you have set a custom error handler function with set_error_handler() then it will still get called,but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @.

在同一页的一条评论中有人写道:

I was confused as to what the @ symbol actually does,and after a few experiments have concluded the following:

  • the error handler that is set gets called regardless of what level the error reporting is set on,or whether the statement is preceeded
    with @

  • it is up to the error handler to impart some meaning on the different error levels. You could make your custom error handler echo
    all errors,even if error reporting is set to NONE.

  • so what does the @ operator do? It temporarily sets the error reporting level to 0 for that line. If that line triggers an error,
    the error handler will still be called,but it will be called with an
    error level of 0

Hope this helps someone

简而言之,如果您使用if(@ $x)而不是isset和empty,您可能看不到任何差异,但幕后有一些额外的工作.这是因为即使您禁止错误,也会始终调用错误处理程序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读