gpt4 book ai didi

php - !empty($x) 和@$x 的区别?

转载 作者:可可西里 更新时间:2023-10-31 22:55:35 25 4
gpt4 key购买 nike

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 .这是因为错误处理程序总是被调用,即使您抑制了错误。

关于php - !empty($x) 和@$x 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19147805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com