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

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'];

What good will it do here?


The @ is the error suppression operator in 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.

See:

  • Error Control Operators
  • Bad uses of the @ operator
  • Update:

    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 .

    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);
    

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


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

    链接地址: http://www.djcxy.com/p/1730.html

    上一篇: PHP函数和@functions

    下一篇: PHP中变量名称前的'At'符号:@ $