Bram Vanroy
Bram Vanroy

Reputation: 28427

Is it possible to disable error reporting only for part of your script?

The PHP documentation reads (emphasis mine):

The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script. If the optional level is not set, error_reporting() will just return the current error reporting level.

Does that mean that the level of error reporting can only be set once in a PHP script, and not changed afterwards? For instance, is it possible to do this:

error_reporting(0);

try {
  errorSilently();
} catch (Exception $e) {
  // Do nothing
}

error_reporting(E_ALL);

try {
  errorLOUDLY();
} catch (Exception $e) {
  // Do nothing
}

Note that I wrote a // Do nothing because it seems that if an error is thrown, Apache writes it to its error_log any way, if it's caught or not. What I want to do is disable that behaviour, and not write to the error log.

Upvotes: 2

Views: 1475

Answers (1)

deceze
deceze

Reputation: 521994

Yes, you can change the error reporting level at any time during your program's runtime, and it will stay at that new level until you change it again. You can "silence" only specific parts. Note that error_reporting() returns the old level, specifically so you can do things like:

$previous = error_reporting(0);

something_which_produces_warnings_which_can_safely_be_ignored();

error_reporting($previous);

To also temporarily disable error logging you may have to ini_set('log_errors', false).

And no, try..catch has nothing to do with error reporting. Those are separate mechanisms. error_reporting influences errors triggered by trigger_error; try..catch is a mechanism for handling exceptions thrown via throw.

Upvotes: 7

Related Questions