4

Possible Duplicates:
Reference - What does this symbol mean in PHP?
What does @ mean in PHP?

I have a line in my code which looks like this:

@mysql_select_db($dbname) or die( "Error: Unable to select database");

It works, but I want to know what the @ does and why it is there.

Community
  • 1
  • 1
Margarez
  • 413
  • 1
  • 4
  • 6
  • 2
    In a great quote I saw recently, "it prevents you from finding out what's wrong with your program". I think it was Gordon... – El Yobo Dec 16 '10 at 11:33
  • @ElYobo dont think it was me, but there is much truth in it – Gordon Dec 16 '10 at 12:30
  • 1
    @Gordon, you should have stayed quiet and took the credit then ;) It was somewhere here on SO, but I can't seem to find it now. – El Yobo Dec 16 '10 at 12:37
  • @ElYobo it was [Ignacio Vazquez-Abrams](http://stackoverflow.com/questions/4393201/what-does-mean-before-a-variable) in the duplicate before this duplicate. I remember I linked it in the reference :) – Gordon Dec 16 '10 at 12:42
  • 2
    "@Gordon, you should have stayed quiet..." Given the question, I find this funny. – Aether Dec 16 '10 at 12:43
  • 2
    @Aether hehe, yeah, but I have [scream](http://us3.php.net/manual/en/book.scream.php) enabled by default ;) – Gordon Dec 16 '10 at 12:46
  • @Gordon, nice detective work. @Aether, you just made me spill my coffee :D – El Yobo Dec 16 '10 at 20:53

3 Answers3

13

The @ symbol suppresses any errors and notices for the expression it precedes.

See this reference: PHP Error Control Operators

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.

Jonathon Bolster
  • 15,221
  • 3
  • 39
  • 46
2

In this case, the @ will suppress the regular PHP database connection error (which may contain sensitive information). In case of a connection error, the "or die" part will be executed, failing with a generic error message. The line is probably copied from a "quick and dirty" example.

Using the error suppression operator @ is considered bad style, especially when other forms of error handling are missing. It complicates debugging - how can you find out about in error without any indication that it occured? In a production system it's better to log all errors to a file and suppress the rendering of errors on the page. You could do that in the php.ini file or (if you are on a shared host and not allowed to make config changes) with the following code.

ini_set('display_errors', false);
ini_set('log_errors', true);
ini_set('error_log', '/var/log/apache/php-errors.log');
chiborg
  • 23,387
  • 11
  • 88
  • 105
1

It suppresses all error output. Generally, you shouldn't use it unless you have a good reason. I don't know why it is used in the example you posted, or why die() is used. The error should be caught and processed accordingly. The select may fail for a number of reasons, some perhaps recoverable. Like no connection to the database established.

Brent Baisley
  • 12,441
  • 2
  • 21
  • 39