php PHP中变量名前的'At'符号:@$_POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3551527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:11:43  来源:igfitidea点击:

'At' symbol before variable name in PHP: @$_POST

phperror-handlingoperators

提问by Majid Fouladpour

I've seen function calls preceded with an at symbol to switch off warnings. Today I was skimming some code and found this:

我见过以 at 符号开头的函数调用来关闭警告。今天我浏览了一些代码,发现了这个:

$hn = @$_POST['hn'];

What good will it do here?

在这里有什么好处?

回答by Sarfraz

The @is the error suppression operator in PHP.

@是PHP错误抑制操作。

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 中添加到表达式之前,该表达式可能生成的任何错误消息都将被忽略。

See:

看:

Update:

更新:

In your example, it is used before the variable name to avoid the E_NOTICEerror there. If in the $_POSTarray, the hnkey is not set; it will throw an E_NOTICEmessage, but @is used there to avoid that E_NOTICE.

在您的示例中,它在变量名称之前使用以避免出现E_NOTICE错误。如果在$_POST数组中,hn则未设置键;它会抛出一条E_NOTICE消息,但@在那里被用来避免这种情况E_NOTICE

Note that you can also put this line on top of your script to avoid an E_NOTICEerror:

请注意,您也可以将此行放在脚本的顶部以避免E_NOTICE错误:

error_reporting(E_ALL ^ E_NOTICE);

回答by Tyson of the Northwest

It won't throw a warning if $_POST['hn'] is not set.

如果 $_POST['hn'] 未设置,它不会发出警告。

回答by SenorPuerco

All that means is that, if $_POST['hn'] is not defined, then instead of throwing an error or warning, PHP will just assign NULL to $hn.

这意味着,如果 $_POST['hn'] 未定义,那么 PHP 不会抛出错误或警告,而是将 NULL 分配给 $hn。

回答by Hydrino

It suppresses warnings if $_POST['something'] is not defined.

如果 $_POST['something'] 未定义,它会抑制警告。