問題描述
我見過以 at 符號開頭的函數調用來關閉警告.今天我瀏覽了一些代碼,發現了這個:
I've seen function calls preceded with an at symbol to switch off warnings. Today I was skimming some code and found this:
$hn = @$_POST['hn'];
它在這里有什么好處?
推薦答案
@
是 PHP 中的錯誤抑制運算符.
The @
is the error suppression operator in PHP.
PHP 支持一種錯誤控制運算符:at 符號 (@).什么時候附加到 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.
見:
- 錯誤控制運算符
- @ 運算符的不當使用
在您的示例中,它用在變量名之前,以避免出現 E_NOTICE
錯誤.如果在$_POST
數組中,沒有設置hn
鍵;它會拋出一個 E_NOTICE
消息,但 @
用于避免 E_NOTICE
.
In your example, it is used before the variable name to avoid the E_NOTICE
error there. If in the $_POST
array, the hn
key is not set; it will throw an E_NOTICE
message, but @
is used there to avoid that E_NOTICE
.
請注意,您也可以將此行放在腳本頂部以避免E_NOTICE
錯誤:
Note that you can also put this line on top of your script to avoid an E_NOTICE
error:
error_reporting(E_ALL ^ E_NOTICE);
這篇關于PHP中變量名前的'At'符號:@$_POST的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!