Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 982
Majid Fouladpour Avatar asked Aug 23 '10 20:08

Majid Fouladpour


People also ask

What is $_ POST in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.

What is before variable in PHP?

Variables are used for storing values such as numeric values, characters, character strings, or memory addresses so that they can be used in any part of the program. Declaring PHP variables. All variables in PHP start with a $ (dollar) sign followed by the name of the variable.

What symbol would you put before a variable name in PHP?

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


1 Answers

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); 
like image 65
Sarfraz Avatar answered Sep 28 '22 07:09

Sarfraz