What does the "@" do in php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techleet
Forum Newbie
Posts: 10
Joined: Wed May 17, 2006 10:51 am
Location: San Jose, CA

What does the "@" do in php?

Post by techleet »

Hi All,

This might be a ridiculous question, but what does the "@" do in php?

I was checking out some code snippets online and saw the following:

Code: Select all

$rs = @mysql_query("SELECT * FROM users WHERE name='bob'");
I've done the same code a million times, but never with the "@" in front... what does it do?

Thanks! :D
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Suppresses any errors it does.

show errors php will not when prefixed with '@' functions are.
User avatar
techleet
Forum Newbie
Posts: 10
Joined: Wed May 17, 2006 10:51 am
Location: San Jose, CA

Post by techleet »

Burrito wrote:Suppresses any errors it does.

show errors php will not when prefixed with '@' functions are.
Damn yoda. 'Splaining code using yoda speak is pretty f'ed-up.

I translated though. Thanks!
User avatar
techleet
Forum Newbie
Posts: 10
Joined: Wed May 17, 2006 10:51 am
Location: San Jose, CA

Post by techleet »

Hmm.

I would think the act of suppressing errors would be a bad habit. Personally I like to know when my proggie doesn't do what it should! I guess it's more for the end user?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Reah this - viewtopic.php?p=215077#215077 - to see how it can be useful
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

techleet wrote:I would think the act of suppressing errors would be a bad habit. Personally I like to know when my proggie doesn't do what it should! I guess it's more for the end user?
Generally, yes, its a bad habit. There are rare exceptions to the rule.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It only surpresses non-fatal errors. Syntactical and fatal errors will still be shown.

I was about to give a code example of where it would be useful but you should really just search around a little or read the link above ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

d11wtq wrote:It only surpresses non-fatal errors. Syntactical and fatal errors will still be shown.
I think this is an important point relating to the code posted in the question. A call to a query gives an error when a connection has not been established or the DB server is unavailable. Both of those can usually be caught by with checks before the query.
(#10850)
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

Roja wrote:
techleet wrote:I would think the act of suppressing errors would be a bad habit. Personally I like to know when my proggie doesn't do what it should! I guess it's more for the end user?
Generally, yes, its a bad habit. There are rare exceptions to the rule.

it's a bad habit only if you don't have your own error handling functions built in...

ie

Code: Select all

$_errors = array();

$sql = "SELECT * FROM mytable";
$rst = @mysql_query($sql);

if(!$rst) {
  throwError(mysql_errno(), 'The query has failed.  Report was : : ' . mysql_error());
}

function throwError($id=false, $message="") {
    global $_errors;
    if ($id === false) {
      $id = mysql_errno();
    }
    
    if (empty($message)) {
      $message = mysql_error();
    }
    
    if ($id !== 0) {
      
      array_push($_errors, array('id'=>$id, 'message'=>$message));

    }
}


if(!empty($_errors)) {
  echo 'There was an error in the sql!  Error dump reports :';
  print_r($_errors);
}


of course this code is untested... but you get the point..

lots of people either don't like php's error reporting, or they would like to impliment their own. suppressing php's error reporting is a good idea and definately not a bad habbit if you have your own error control. otherwise, yeah, it's kinda dumb and is a bad habit because you then have no way of knowing what went wrong.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

mudvein wrote:
Roja wrote:Generally, yes, its a bad habit. There are rare exceptions to the rule.
it's a bad habit only if you don't have your own error handling functions built in...
And thats a rare exception to the rule. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

For anyone who's tempted to go nuts with error surpressing: If you're using PHP5 I strongly suggest you take a look into Exception handling (try/catch).
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

techleet wrote:
Burrito wrote:Suppresses any errors it does.

show errors php will not when prefixed with '@' functions are.
Damn yoda. 'Splaining code using yoda speak is pretty f'ed-up.

I translated though. Thanks!
Edit: I got it... :oops: your name is techleet :P

Ooops

Your sig...techleet...I dont' get it :?
Anglophobe
Forum Newbie
Posts: 11
Joined: Tue May 09, 2006 11:50 am

Post by Anglophobe »

I think I got it too...any more to it than 64-bit?

I seriously thought about converting to decimal for about thirty seconds before deciding against it.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Anglophobe wrote:I think I got it too...any more to it than 64-bit?

I seriously thought about converting to decimal for about thirty seconds before deciding against it.
:? Wrong thread?
Anglophobe
Forum Newbie
Posts: 11
Joined: Tue May 09, 2006 11:50 am

Post by Anglophobe »

Confused Wrong thread?
No, sorry, the name :?
Post Reply