PHP Rapid-Fire: What is the @ symbol used for in PHP?

The @ symbol is used in PHP to suppress error warnings in your code. Prepending an ampersand to a line of PHP will suppress the error from reporting but it doe’s come with a few caveats listed below.

Example: To stop errors from showing for a class

 $var = @new some_class(); 

Example: Stop error messages from showing if a key doesn’t exists in an array

 $value = @$array[$key]; 

Although I never recommend using the @ symbol, if you are going to use it to stop error messages from being generated there are a few things to consider:

  1. A common misconception is that the error control operator (@) stops the error handler from being triggered, but it doesn’t. If the line triggers an error the @ operator simply sets the error level to 0 and the error handler then interprets that. If you are creating your own error handler you will need to account for this otherwise the error message will still fire.
  2. When calling the @ before an include, creating a new object or calling a function it will set the error reporting to 0 for the entire file/object/function therefore suppressing any errors from being shown.

Leave a Reply

Your email address will not be published. Required fields are marked *