views:

67

answers:

2

I have a custom error handler attached to E_ALL PHP errors.

In an external lib the call $row = @mysql_fetch_assoc($this->result); triggers PHP Warnings caught by my handler. Why? Shouldn't '@' make PHP ignore this?

My question: Is there any way I can detect (in my error handler) that '@' was used?

+3  A: 

A quick look into the PHP manual revealed this:

It is important to remember that the standard PHP error handler is completely bypassed. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.

http://de.php.net/manual/en/function.set-error-handler.php

Using the @ operator is considered bad style by quite some people, btw.

KiNgMaR
There are some entirely valid cases for using @ though, like with unlink() when you don't care if the file exists or not. I'll agree it can be overused but there are definitely valid cases for it.
cletus
+4  A: 

Referencing the PHP Manual on set_error_handler shows

It is important to remember that the standard PHP error handler is completely bypassed. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.

So you could do this:

function my_error_handler($errno, $errstr) {
  if (error_reporting() == 0) { // called with @
    return;
  }
  //....
}
gnarf