PHP中的@符号有什么用?

发布于 2024-07-25 01:22:37 字数 136 浏览 2 评论 0原文

我见过在某些函数前面使用 @ ,如下所示:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

没有心的人 2024-08-01 01:22:37

它会抑制错误消息 - 请参阅 PHP 手册中的错误控制运算符

It suppresses error messages — see Error Control Operators in the PHP manual.

浅唱々樱花落 2024-08-01 01:22:37

它抑制错误。

请参阅手册中的错误控制运算符

PHP 支持一种错误控制运算符:at 符号 (@)。 当添加到 PHP 中的表达式之前时,该表达式可能生成的任何错误消息都将被忽略。

如果您使用 set_error_handler()< 设置了自定义错误处理函数/a> 那么它仍然会被调用,但是这个自定义错误处理程序可以(并且应该)调用 error_reporting() 当触发错误的调用前面有 @... 时,它将返回 0

It suppresses errors.

See Error Control Operators in 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 @...

夏九 2024-08-01 01:22:37

@ 符号是错误控制操作员(又名“沉默”或“闭嘴”操作员)。 它使 PHP 抑制与其关联的表达式生成的任何诊断错误消息。 与一元运算符一样,它具有优先级和结合性。 以下是一些示例:

@echo 1 / 0;
// displays "Parse error: syntax error, unexpected T_ECHO"
// this is because "echo" is not an expression
// the script terminates because of parse error

echo @(1 / 0);
// suppressed "Warning: Division by zero"

@$i / 0;
// suppressed "Notice: Undefined variable: i"
// displays "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";
// displays "Fatal error: Call to undefined function foobar()"
// fatal errors are displayed even with the shut-up operator
// the script terminates because of fatal error

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:

@echo 1 / 0;
// displays "Parse error: syntax error, unexpected T_ECHO"
// this is because "echo" is not an expression
// the script terminates because of parse error

echo @(1 / 0);
// suppressed "Warning: Division by zero"

@$i / 0;
// suppressed "Notice: Undefined variable: i"
// displays "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";
// displays "Fatal error: Call to undefined function foobar()"
// fatal errors are displayed even with the shut-up operator
// the script terminates because of fatal error
挽梦忆笙歌 2024-08-01 01:22:37

另请注意,尽管隐藏了错误,但任何自定义错误处理程序(使用 set_error_handler 设置)仍将被执行!

Also note that despite errors being hidden, any custom error handler (set with set_error_handler) will still be executed!

不离久伴 2024-08-01 01:22:37

就像之前有人回答的那样: @ 运算符抑制 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.

不气馁 2024-08-01 01:22:37

@ 抑制错误消息。

它用在如下代码片段中:

@file_get_contents('http://www.exaple.com');

如果无法访问域“http://www.exaple.com”,则会显示错误,但使用 @ 则不会显示任何内容。

@ 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 shown.

萌︼了一个春 2024-08-01 01:22:37

假设我们没有使用“@”运算符,那么我们的代码将如下所示:

$fileHandle = fopen($fileName, $writeAttributes);

如果找不到我们尝试打开的文件怎么办? 它将显示一条错误消息。

为了抑制错误消息,我们使用“@”运算符,例如:

$fileHandle = @fopen($fileName, $writeAttributes);

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);
Oo萌小芽oO 2024-08-01 01:22:37

如果打开失败,则会生成 E_WARNING 级别的错误。 您可以使用@来抑制此警告。

If the open fails, an error of level E_WARNING is generated. You may use @ to suppress this warning.

寂寞陪衬 2024-08-01 01:22:37

PHP 支持一种错误控制运算符:at 符号(@)。 当添加到 PHP 中的表达式之前时,该表达式可能生成的任何错误消息都将被忽略。

如果您使用 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.

?>

注意:-

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) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @.

<?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.

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:-

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.

清音悠歌 2024-08-01 01:22:37

可能值得在这里添加一些在使用@时您应该注意的提示,为了完整地查看这篇文章:http://mstd.eu/index.php/2016/06/30/ php-rapid-fire-what-is-the-symbol-used-for-in-php/

  1. 即使前面添加了@符号,错误处理程序仍然会被触发,它只意味着错误级别为0设置了,这必须在自定义错误处理程序中进行适当处理。

  2. 在包含前面加上 @ 会将包含文件中的所有错误设置为错误级别 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/

  1. 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.

  2. Prepending a include with @ will set all errors in the include file to an error level of 0

一口甜 2024-08-01 01:22:37

@ 抑制函数抛出的错误消息。 当文件不存在时,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文