error_reporting

Sets which error levels PHP reports and returns the level that was in effect before.

Signature
error_reporting(?int $error_level = null): int
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$error_leveloptional?int= nullThe new reporting level as a bitmask of E_* constants. If null or omitted, the level is left unchanged and only reported.

Return value

intThe old error reporting level, or the current level if error_level is null.

Changelog

  • PHP 8.0.0error_level is nullable now.
  • PHP 8.4.0The E_STRICT constant is deprecated; including it in a mask emits a deprecation notice.

error_reporting() controls which error levels PHP acts on at runtime, overriding the error_reporting ini setting for the rest of the request. It always returns the previous level, which is what makes it safe to change temporarily and put back.

Reporting is not displaying

This is the classic confusion. error_reporting() decides which errors are considered; display_errors decides whether they are printed, and log_errors whether they are written to the log. Setting E_ALL on a production server with display_errors=Off shows nothing on screen and everything in the log — which is exactly what you want. Turning errors “on” usually means setting both.

Building masks

Combine constants with | and remove them with & ~, for example E_ALL & ~E_DEPRECATED. Passing -1 enables every level including ones added in future PHP releases, which is more future-proof than E_ALL in a hardcoded config. Since PHP 8.0, E_ALL already includes E_STRICT-style diagnostics, and in 8.4 the E_STRICT constant itself became deprecated — drop it from old masks.

Two limits: parse errors in the current file happen before your call runs, and once a custom error handler is installed it is called for every level regardless of this setting.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Enable E_ALL in a development bootstrap while keeping production on the ini default
  • Silence deprecation notices from a legacy dependency without touching php.ini
  • Temporarily lower the level around one noisy third-party call and restore it afterwards

Frequently asked questions

How do I show all PHP errors on screen?

Set both error_reporting(E_ALL) and ini_set('display_errors', '1'); reporting alone only decides which errors PHP considers, not whether they are printed. Never enable display_errors in production.

What is the difference between error_reporting and display_errors?

error_reporting() picks which error levels PHP acts on, while display_errors controls whether those errors are written into the output. An error must pass the level filter first and then the display setting.

Does error_reporting(0) affect my custom error handler?

No. A handler set with set_error_handler() is called for every error regardless of the level; the setting only changes the value error_reporting() returns inside it, so filter in the handler yourself.

More examples

Restore the level after a noisy call

Because error_reporting() returns the old level, the finally block can put it back exactly as it was even if the callback throws. This is the safe pattern for wrapping a single legacy call rather than lowering the level globally.

Example · php
<?php
function quietly(callable $fn): mixed {
    $old = error_reporting(E_ALL & ~E_WARNING & ~E_DEPRECATED);
    try {
        return $fn();
    } finally {
        error_reporting($old);
    }
}

$value = quietly(fn() => 'result from legacy code');
echo $value, "\n";
var_dump(error_reporting() === E_ALL || true);

Reading a level without changing it

Calling it with no argument reports the current mask without modifying it. Testing each constant with a bitwise AND is how you inspect a level, since the raw integer is meaningless on its own.

Example · php
<?php
$level = error_reporting();

$checks = ['E_ERROR' => E_ERROR, 'E_WARNING' => E_WARNING, 'E_NOTICE' => E_NOTICE, 'E_DEPRECATED' => E_DEPRECATED];

foreach ($checks as $name => $bit) {
    echo str_pad($name, 14), ($level & $bit) ? "on\n" : "off\n";
}

The @ operator and the current level

Inside the handler, error_reporting() returns a reduced mask for the @-suppressed call and the full mask for the normal one. That comparison is the documented way for a custom handler to tell whether the error was suppressed at the call site.

Example · php
<?php
error_reporting(E_ALL);

set_error_handler(function (int $no, string $msg): bool {
    echo 'handler saw level ', error_reporting(), ": {$msg}\n";
    return true;
});

@trigger_error('suppressed call', E_USER_WARNING);
trigger_error('normal call', E_USER_WARNING);
restore_error_handler();

Discussion

  • Be the first to comment on this lesson.