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

Viewed 40581

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?

5 Answers

I'm answering 11 years later for completeness regarding modern php.

Since php 7.0, the null coalescing operator is a more straightforward alternative to silencing warnings in that case. The ?? operator was designed (among other things) for that purpose.

Without @, a warning is shown:

$ php -r 'var_dump($_POST["hn"]);'
PHP Warning:  Undefined array key "hn" in Command line code on line 1
NULL

The output with silencing warnings (@):

$ php -r 'var_dump(@$_POST["hn"]);'
NULL

Obtaining the same result with the modern null coalescing operator (??):

$ php -r 'var_dump($_POST["hn"] ?? null);'
NULL
Related