1

Is there any way to find out if this warning was supressed or not using the set_error_handler() function? The code is 2 for @somethingwrong() and somethingwrong(). But I don't want to log all warnings. For example some functions like gzuncompress() must be handled this way because I have no way to test for valid input data.

$handler = new errorHandler();
error_reporting(0);
set_error_handler(array($handler, 'handle'));
set_exception_handler(array($handler, 'exception'));
register_shutdown_function(array($handler, 'shutdown'));

class errorHandler
{
public function handle($code, $text, $file, $line)
{
    if (($code & (E_STRICT | E_NOTICE)) == 0)
        $this->log(...);
}

public function exception($exception)
{
    ...
}

public function shutdown()
{
    $e = error_get_last();
    if ($e !== NULL)
        $this->log($error);
}
}

// the errors
include('notexisting.file'); // should be logged
@gzuncompress('somenonsens'); // should not be logged
  • Which "this warning" do you want to suppress? With error_reporting(0) you are disabling error reporting. Perhaps you meant error_reporting(-1) or similar? What code is 2 for @somethingwrong()? Please clarify your question. – tomsv May 14 '13 at 14:29
  • As you can see on php.net code 2 is for E_WARNING, 4 for E_PARSE, ... If you say it is disabling then why does it still log all errors? But it turned off displaying errors. Now I wrote ini_set('display_errors','Off'); error_reporting(-1); The effect is the same. – user2381982 May 15 '13 at 08:57

2 Answers2

0

Did you try it? I think if you use @ as in @gzuncompress you will not get a normal warning and you will also not have your errorhandler called.

tomsv
  • 7,207
  • 6
  • 55
  • 88
  • Yes, I did. That's the reason I've asked. It does not influence the reporting. Maybe it is something that is controlled by some ini-entries? – user2381982 May 15 '13 at 08:47
  • The PHP Manual states that the error handler will be called regardless of the use of the at sign, and indeed this happens. I will write an answer if I figure one out. – David Spector Jun 28 '21 at 15:34
0

I found a solution. Initially, I call

error_reporting(E_USER_ERROR);

This suppresses all errors except one, which I avoid raising myself, so it should never happen. I do this because I intercept all errors myself, reporting them in any of four ways (to screen, by email, to local log, to PHP log). I also do this to avoid an error reporting level of 0.

Then, in an error handler or shutdown function, I can detect the use of an at sign by the fact that it changes the error reporting level:

if (error_reporting() != E_USER_ERROR)
    return; // Ignore this error

Note that the effect of at sign changes between PHP version 7 and 8. I have not tested this with version 8.

David Spector
  • 1,520
  • 15
  • 21