PHP中的@符号有什么用?
我见过在某些函数前面使用 @
,如下所示:
$fileHandle = @fopen($fileName, $writeAttributes);
这个符号有什么用?
I have seen uses of @
in front of certain functions, like the following:
$fileHandle = @fopen($fileName, $writeAttributes);
What is the use of this symbol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
它会抑制错误消息 - 请参阅 PHP 手册中的错误控制运算符。
It suppresses error messages — see Error Control Operators in the PHP manual.
它抑制错误。
请参阅手册中的错误控制运算符:
如果您使用 set_error_handler()< 设置了自定义错误处理函数/a> 那么它仍然会被调用,但是这个自定义错误处理程序可以(并且应该)调用 error_reporting() 当触发错误的调用前面有 @... 时,它将返回 0
It suppresses errors.
See Error Control Operators in the manual:
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 @...
@
符号是错误控制操作员(又名“沉默”或“闭嘴”操作员)。 它使 PHP 抑制与其关联的表达式生成的任何诊断错误消息。 与一元运算符一样,它具有优先级和结合性。 以下是一些示例:The
@
symbol is the error control operator (aka the "silence" or "shut-up" operator). It makes PHP suppress any diagnostic error messages generated by the expression associated with it. Like unary operators, it has a precedence and associativity. Below are some examples:另请注意,尽管隐藏了错误,但任何自定义错误处理程序(使用
set_error_handler
设置)仍将被执行!Also note that despite errors being hidden, any custom error handler (set with
set_error_handler
) will still be executed!就像之前有人回答的那样:
@
运算符抑制 PHP 中的所有错误,包括通知、警告甚至严重错误。但是: 拜托,真的不要使用
@
运算符。为什么?
嗯,因为当您使用
@
运算符进行错误抑制时,发生错误时您完全不知道从哪里开始。 我已经对遗留代码有了一些“乐趣”,其中一些开发人员经常使用@
运算符。 特别是在文件操作、网络调用等情况下。在这些情况下,许多开发人员都建议使用@
运算符,因为当此处发生错误时,这有时会超出范围(例如3rdparty API 可能无法访问等)。但仍然不使用它有什么意义呢? 让我们从两个角度来看一下:
作为开发者:当使用
@
时,我完全不知道从哪里开始。 如果有数百甚至数千个使用@
的函数调用,则错误可能就像 everyhwere 一样。 在这种情况下不可能进行合理的调试。 即使这只是第 3 方错误 - 那么也没关系,您很快就能完成。 ;-) 此外,最好在错误日志中添加足够的详细信息,以便开发人员能够轻松决定日志条目是否是必须进一步检查的内容,或者是否只是超出开发人员范围的第 3 方故障。作为用户:用户根本不关心错误的原因是什么。 软件是供他们工作、完成特定任务等的。他们不在乎这是开发人员的错还是第三方的问题。 特别是对于用户,我强烈建议记录所有错误,即使它们超出了范围。 也许您会注意到某个特定的 API 经常离线。 你能做什么? 您可以与您的 API 合作伙伴联系,如果他们无法保持稳定,您可能应该寻找其他合作伙伴。
简而言之:你应该知道存在像
@
这样的东西(知识总是好的),但只是不要使用它。 许多开发人员(尤其是调试其他人的代码的开发人员)将非常感激。Like already some answered before: The
@
operator suppresses all errors in PHP, including notices, warnings and even critical errors.BUT: Please, really do not use the
@
operator at all.Why?
Well, because when you use the
@
operator for error supression, you have no clue at all where to start when an error occurs. I already had some "fun" with legacy code where some developers used the@
operator quite often. Especially in cases like file operations, network calls, etc. Those are all cases where lots of developers recommend the usage of the@
operator as this sometimes is out of scope when an error occurs here (for example a 3rdparty API could be unreachable, etc.).But what's the point to still not use it? Let's have a look from two perspectives:
As a developer: When
@
is used, I have absolutely no idea where to start. If there are hundreds or even thousands of function calls with@
the error could be like everyhwere. No reasonable debugging possible in this case. And even if it is just a 3rdparty error - then it's just fine and you're done fast. ;-) Moreover, it's better to add enough details to the error log, so developers are able to decide easily if a log entry is something that must be checked further or if it's just a 3rdparty failure that is out of the developer's scope.As a user: Users don't care at all what the reason for an error is or not. Software is there for them to work, to finish a specific task, etc. They don't care if it's the developer's fault or a 3rdparty problem. Especially for the users, I strongly recommend to log all errors, even if they're out of scope. Maybe you'll notice that a specific API is offline frequently. What can you do? You can talk to your API partner and if they're not able to keep it stable, you should probably look for another partner.
In short: You should know that there exists something like
@
(knowledge is always good), but just do not use it. Many developers (especially those debugging code from others) will be very thankful.@
抑制错误消息。它用在如下代码片段中:
如果无法访问域“http://www.exaple.com”,则会显示错误,但使用
@
则不会显示任何内容。@
suppresses error messages.It is used in code snippets like:
If domain "http://www.exaple.com" is not accessible, an error will be shown, but with
@
nothing is shown.假设我们没有使用“@”运算符,那么我们的代码将如下所示:
如果找不到我们尝试打开的文件怎么办? 它将显示一条错误消息。
为了抑制错误消息,我们使用“@”运算符,例如:
Suppose we haven't used the "@" operator then our code would look like this:
And what if the file we are trying to open is not found? It will show an error message.
To suppress the error message we are using the "@" operator like:
如果打开失败,则会生成 E_WARNING 级别的错误。 您可以使用@来抑制此警告。
If the open fails, an error of level E_WARNING is generated. You may use @ to suppress this warning.
PHP 支持一种错误控制运算符:at 符号
(@)
。 当添加到 PHP 中的表达式之前时,该表达式可能生成的任何错误消息都将被忽略。如果您使用
set_error_handler()
设置了自定义错误处理函数,那么它仍然会被调用,但是这个自定义错误处理程序可以(并且应该)调用error_reporting()
,这将当触发错误的调用前面有@
时,返回0
。注意:-
1) @运算符仅适用于表达式。
2)一个简单的经验法则是:如果您可以获取某个值,则可以在其前面添加 @ 运算符。 例如,您可以将其添加到变量、函数之前并包含调用、常量等。 您不能将其添加到函数或类定义或条件结构(例如 if 和 foreach 等)之前。
警告:-
PHP supports one error control operator: the at sign
(@)
. When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.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) callerror_reporting()
which will return0
when the call that triggered the error was preceded by an@
.Note:-
1) The @-operator works only on expressions.
2) A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.
Warning:-
可能值得在这里添加一些在使用@时您应该注意的提示,为了完整地查看这篇文章:http://mstd.eu/index.php/2016/06/30/ php-rapid-fire-what-is-the-symbol-used-for-in-php/
即使前面添加了@符号,错误处理程序仍然会被触发,它只意味着错误级别为0设置了,这必须在自定义错误处理程序中进行适当处理。
在包含前面加上 @ 会将包含文件中的所有错误设置为错误级别 0
It might be worth adding here there are a few pointers when using the @ you should be aware of, for a complete run down view this post: http://mstd.eu/index.php/2016/06/30/php-rapid-fire-what-is-the-symbol-used-for-in-php/
The error handler is still fired even with the @ symbol prepended, it just means a error level of 0 is set, this will have to be handled appropriately in a custom error handler.
Prepending a include with @ will set all errors in the include file to an error level of 0
@
抑制函数抛出的错误消息。 当文件不存在时,fopen
会抛出错误。 即使文件不存在,@
符号也会使执行点移动到下一行。 我的建议是,当您开发 PHP 代码时,不要在本地环境中使用它。@
suppresses the error message thrown by the function.fopen
throws an error when the file doesn't exist. The@
symbol makes the execution point to move to the next line even if the file doesn't exist. My suggestion would be no to use this in your local environment when you develop a PHP code.