php PHP中@符号的作用是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1032161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of the @ symbol in PHP?
提问by sv_in
I have seen uses of @in front of certain functions, like the following:
我@在某些函数前面看到过使用,如下所示:
$fileHandle = @fopen($fileName, $writeAttributes);
What is the use of this symbol?
这个符号有什么用?
回答by RichieHindle
It suppresses error messages — see Error Control Operatorsin the PHP manual.
它抑制错误消息 - 请参阅PHP 手册中的错误控制运算符。
回答by Aiden Bell
It suppresses errors.
它抑制错误。
See Error Control Operatorsin the manual:
请参阅手册中的错误控制运算符:
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) call error_reporting()which will return 0 when the call that triggered the error was preceded by an @...
PHP 支持一种错误控制运算符:at 符号 (@)。在 PHP 中添加到表达式之前,该表达式可能生成的任何错误消息都将被忽略。
如果您使用set_error_handler()设置了自定义错误处理函数,那么它仍然会被调用,但是这个自定义错误处理函数可以(并且应该)调用error_reporting(),当触发错误的调用之前是 @ 时,它将返回 0 ...
回答by Salman A
The @symbol is the error control operator(aka the "silence" or "shut-up" operator). It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity. Below are some examples:
的@符号是差错控制操作(也称为“沉默”或“关上”操作符)。它使 PHP 抑制相关表达式生成的任何错误消息(通知、警告、致命等)。它的工作原理就像一元运算符,例如,它具有优先级和结合性。下面是一些例子:
@echo 1 / 0;
// generates "Parse error: syntax error, unexpected T_ECHO" since
// echo is not an expression
echo @(1 / 0);
// suppressed "Warning: Division by zero"
@$i / 0;
// suppressed "Notice: Undefined variable: i"
// displayed "Warning: Division by zero"
@($i / 0);
// suppressed "Notice: Undefined variable: i"
// suppressed "Warning: Division by zero"
$c = @$_POST["a"] + @$_POST["b"];
// suppressed "Notice: Undefined index: a"
// suppressed "Notice: Undefined index: b"
$c = @foobar();
echo "Script was not terminated";
// suppressed "Fatal error: Call to undefined function foobar()"
// however, PHP did not "ignore" the error and terminated the
// script because the error was "fatal"
What exactly happens if you use a custom error handler instead of the standard PHP error handler:
如果您使用自定义错误处理程序而不是标准 PHP 错误处理程序,究竟会发生什么:
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 @.
如果您使用 set_error_handler() 设置了自定义错误处理函数,那么它仍然会被调用,但是这个自定义错误处理函数可以(并且应该)调用 error_reporting() ,当触发错误的调用之前是 @ 时,它将返回 0 .
This is illustrated in the following code example:
下面的代码示例说明了这一点:
function bad_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
echo "[bad_error_handler]: $errstr";
return true;
}
set_error_handler("bad_error_handler");
echo @(1 / 0);
// prints "[bad_error_handler]: Division by zero"
The error handler did not check if @symbol was in effect. The manual suggests the following:
错误处理程序不检查@符号是否有效。手册建议如下:
function better_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
if(error_reporting() !== 0) {
echo "[better_error_handler]: $errstr";
}
// take appropriate action
return true;
}
回答by nickf
Also note that despite errors being hidden, any custom error handler (set with set_error_handler) will still be executed!
另请注意,尽管隐藏了错误,但set_error_handler仍将执行任何自定义错误处理程序(使用 设置)!
回答by Andreas
Like already some answered before: The @operator suppresses all errors in PHP, including notices, warnings and even critical errors.
就像之前已经回答过的那样:@操作符会抑制 PHP 中的所有错误,包括通知、警告甚至严重错误。
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.).
好吧,因为当您使用@运算符进行错误抑制时,您根本不知道发生错误时从哪里开始。我已经对遗留代码有了一些“乐趣”,其中一些开发@人员经常使用运算符。特别是在文件操作、网络调用等情况下。 这些都是许多开发人员推荐使用@operator的情况,因为当这里发生错误时,这有时超出范围(例如,3rdparty API 可能无法访问等)。 )。
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.
作为开发人员:何时@使用,我完全不知道从哪里开始。如果有成百上千个函数调用@出现错误可能就像每个人一样。在这种情况下无法进行合理的调试。即使它只是一个 3rdparty 错误 - 那么它就很好并且你完成得很快。;-) 此外,最好在错误日志中添加足够的详细信息,以便开发人员能够轻松决定日志条目是必须进一步检查的内容,还是只是超出开发人员范围的 3rdparty 故障。
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.
作为用户:用户根本不关心错误的原因是什么。软件是为他们工作、完成特定任务等提供的。他们不在乎是开发人员的错还是第 3 方的问题。特别是对于用户,我强烈建议记录所有错误,即使它们超出范围。也许您会注意到某个特定的 API 经常离线。你能做什么?您可以与您的 API 合作伙伴交谈,如果他们无法保持稳定,您可能应该寻找其他合作伙伴。
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.
简而言之:你应该知道存在这样的东西@(知识总是好的),但不要使用它。许多开发人员(尤其是那些调试他人代码的开发人员)会非常感激。
回答by Sujeet Kumar
Suppose we haven't used the "@" operator then our code would look like this:
假设我们没有使用“@”运算符,那么我们的代码将如下所示:
$fileHandle = fopen($fileName, $writeAttributes);
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:
为了抑制错误消息,我们使用了“@”运算符,例如:
$fileHandle = @fopen($fileName, $writeAttributes);
回答by fico7489
@suppresses error messages.
@抑制错误消息。
It is used in code snippets like:
它用于代码片段,例如:
@file_get_contents('http://www.exaple.com');
If domain "http://www.exaple.com" is not accessible, an error will be shown, but with @nothing is not showed.
如果无法访问域“ http://www.exaple.com”,则会显示错误,但@不会显示任何内容。
回答by Kishor Singh
If the open fails, an error of level E_WARNING is generated. You may use @ to suppress this warning.
如果打开失败,则会生成 E_WARNING 级别的错误。您可以使用 @ 来取消此警告。
回答by Ravi Hirani
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.
PHP 支持一种错误控制运算符: at 符号(@)。在 PHP 中添加到表达式之前,该表达式可能生成的任何错误消息都将被忽略。
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 0when the call that triggered the error was preceded by an @.
如果您设置了一个自定义错误处理函数,set_error_handler()那么它仍然会被调用,但是这个自定义错误处理函数可以(并且应该)调用error_reporting(),0当触发错误的调用之前是@.
<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
die ("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note:-
笔记:-
1) The @-operator works only on expressions.
1) @-operator 仅适用于表达式。
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.
2) 一个简单的经验法则是:如果您可以获取某物的值,则可以在其前面添加 @ 运算符。例如,您可以将其添加到变量、函数和包含调用、常量等之前。您不能将其添加到函数或类定义或条件结构(如 if 和 foreach 等)中。
Warning:-
警告:-
Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
目前,“@”错误控制运算符前缀甚至会禁用将终止脚本执行的严重错误的错误报告。除此之外,这意味着如果您使用“@”来抑制某个函数的错误,并且它不可用或输入错误,则脚本将立即死在那里,没有任何迹象表明原因。
回答by twigg
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/
可能值得在此处添加一些提示,使用 @ 时您应该注意,要完整查看此帖子:http: //mstd.eu/index.php/2016/06/30/php- Rapid-fire-what-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
即使在前面加上了 @ 符号,错误处理程序仍然会被触发,这只是意味着错误级别设置为 0,这必须在自定义错误处理程序中进行适当处理。
在包含前添加 @ 会将包含文件中的所有错误设置为错误级别 0

