Page 1 of 1

What does the "@" do in php?

Posted: Wed May 17, 2006 10:54 am
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

Posted: Wed May 17, 2006 11:04 am
by Burrito
Suppresses any errors it does.

show errors php will not when prefixed with '@' functions are.

Posted: Wed May 17, 2006 11:08 am
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!

Posted: Wed May 17, 2006 11:11 am
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?

Posted: Wed May 17, 2006 11:16 am
by JayBird
Reah this - viewtopic.php?p=215077#215077 - to see how it can be useful

Posted: Wed May 17, 2006 12:18 pm
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.

Posted: Wed May 17, 2006 1:52 pm
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 ;)

Posted: Wed May 17, 2006 2:35 pm
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.

Posted: Wed May 17, 2006 4:26 pm
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.

Posted: Wed May 17, 2006 4:28 pm
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. :)

Posted: Wed May 17, 2006 5:32 pm
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).

Posted: Wed May 17, 2006 7:09 pm
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 :?

Posted: Wed May 17, 2006 9:09 pm
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.

Posted: Thu May 18, 2006 1:03 am
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?

Posted: Thu May 18, 2006 7:45 am
by Anglophobe
Confused Wrong thread?
No, sorry, the name :?