Bram Vanroy Bram Vanroy - 2 months ago 11
PHP Question

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:


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


try {
} 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
any way, if it's caught or not. What I want to do is disable that behaviour, and not write to the error log.


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);



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.