PHP error_reporting() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

In PHP, error_reporting() sets which kinds of errors PHP should report, and it returns the previous setting. Called with no argument, it just tells you the current level.

PHP has many kinds of errors, such as notices, warnings, deprecations and fatal errors. Each kind has a constant like E_WARNING, and the level is a number built from those constants. While developing you usually want E_ALL, so nothing is hidden.

Syntax

Call error_reporting() with the values you need. Start with this line.

PHP
error_reporting(E_ALL);
CallWhat it does
error_reporting(E_ALL)Reports every kind of error.
error_reporting(E_ALL & ~E_NOTICE)Reports everything except notices.
error_reporting()Returns the current level without changing it.

Use & ~ to remove a level from a group, and | to add levels together. In PHP 8 the value of E_ALL is 32767. This function only changes what PHP reports. Whether messages appear on screen is controlled by display_errors.

Library declarations
PHP
error_reporting(?int $error_level = null): int

Parameters

ParameterDescription
error_levelOptional. The new error reporting level, built from constants such as E_ALL, E_ERROR, E_WARNING and E_NOTICE. If it is null (the default), the level is not changed and the current level is returned.

Return Value

Return ValueDescription
intThe old error reporting level, as it was before the call. When no argument is given, this is the current level.

PHP Examples

Example 1 — Setting and Reading the Level

We set the level to E_ALL, read it back, and then remove two kinds of errors from the list.

PHP
<?php
error_reporting(E_ALL);
echo "E_ALL level: ", error_reporting(), PHP_EOL;

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
echo "Without notices and deprecations: ", error_reporting(), PHP_EOL;

How It Works

  1. error_reporting(E_ALL) turns on every kind of error.
  2. Called with no argument, error_reporting() returns the current level, which is 32767 for E_ALL in PHP 8.
  3. E_ALL & ~E_NOTICE & ~E_DEPRECATED starts from E_ALL and switches off the notice bit (8) and the deprecated bit (8192).
  4. 32767 minus 8 minus 8192 leaves 24567.

Example 2 — Checking a Level with a Bitmask

Use the & operator to test whether a certain kind of error is currently reported.

PHP
<?php
error_reporting(E_ALL & ~E_NOTICE);

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

foreach ($levels as $name => $value) {
    echo $name, ": ", (error_reporting() & $value) ? 'on' : 'off', PHP_EOL;
}

How It Works

  1. The level is set to everything except notices.
  2. The array pairs each name with its constant, so we can loop over them.
  3. error_reporting() & $value is non-zero only when that kind of error is included in the level.
  4. Only E_NOTICE comes out as off, because we removed it.

Example 3 — Turning Reporting Off and Restoring It

error_reporting() returns the old level, so you can switch reporting off for a moment and put it back afterwards.

PHP
<?php
$old = error_reporting(0);

echo "Now: ", error_reporting(), PHP_EOL;
echo $undefinedVariable;
echo "No warning was shown", PHP_EOL;

error_reporting($old);
echo "Restored: ", error_reporting() === $old ? 'yes' : 'no', PHP_EOL;

How It Works

  1. error_reporting(0) switches reporting off and returns the previous level, which we save in $old.
  2. Echoing an undefined variable would normally cause a warning, but nothing is reported now.
  3. The script prints its own message to show it kept running.
  4. error_reporting($old) puts the original level back, and the comparison confirms it.

Frequently Asked Questions

It sets which kinds of errors PHP reports. It returns the old level, and when you call it without arguments it returns the current level.
Use error_reporting(E_ALL). It reports every kind of problem, which helps you find and fix mistakes early.
Use error_reporting(E_ALL & ~E_NOTICE). The & ~ combination removes one level from the group.
error_reporting() chooses which errors PHP cares about. display_errors decides whether those errors are printed on the page. On a live site you normally keep reporting on but turn display_errors off.
It switches off reporting for all errors. It can hide problems and make debugging very hard, so avoid it except for short, careful use.

Next: get_error_handler()

get_error_handler() returns the error handler that is currently set, or null if none is se.

get_error_handler() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful