81

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

I was wondering what @ means in PHP language. I have seen people using

$connect = @mysql_query('sql query here'); 

Not sure why. Could someone explain it for me?

2
  • 6
    it's a bad, bad thing. Never use it and eliminate every one you find. Commented Sep 1, 2010 at 19:08
  • 7
    Don't say "never". I can think of a particular example with ldap when testing a user's credentials. If the credentials fail, an error is printed and cannot be trapped with a try/catch. The only thing you can do is test the result. While typically I'd agree with you, there ARE exceptions. Commented Sep 2, 2010 at 2:02

5 Answers 5

72

The @ operator tells PHP to suppress error messages, so that they will not be shown.

For instance, using:

$result = mysql_query("this is an invalid query"); 

would result in a warning being shown, telling you that the MySQL query is invalid, while

$result = @mysql_query("this is still an invalid query"); 

would not.

Note, however, that this is very bad programming practice as it does not make error disappear, it just hides them, and it makes debugging a heck of a lot worse since you can't see what's actually wrong with your code.

Instead of using @, you should disable error_reporting and display_errors just display_errors in php.ini

Sign up to request clarification or add additional context in comments.

5 Comments

Good use of actual examples, thanks for this answer.
accepted answer for very detailed explanation ...
One note. Never disable error_reporting. You take this setting wrong. display_errors is one you need in this case.
I agree with the colonel. Log_errors is important in applications. And just so misbehaving 3rd party libraries are forced to play along we have scream, which disables the @: php.net/manual/en/scream.examples-simple.php
As of PHP 8.0 it doesn't suppress Fatal errors.
16

The @ sign tells PHP to ignore error messages.

PHP Error Control Operators

Comments

2

It's an error control operator.

Comments

1

The @ is a way to tell that you don't want to print error messages. It's a bad practice because you might have an error and never see it because you just "hid" it.

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.


Resources :

Comments

0

@ Operator = Indicates that if there is any kind of error occur, then don't display the message in the user's browser. There are people who test this and didn't see the difference even they put the @ or not they still don't see any error in the browser, well just to add up, the reason why this happen is because of the setting in the php.ini file for error output is turn off.

Different hosting company have different setting so to make sure that you don't want to see any ugly script error for the users or hackers(for them to give a clue to infiltrate you site) you can always use the @ operator.

Hope this help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.