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);
Call
What 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
Parameter
Description
error_level
Optional. 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 Value
Description
int
The 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.
error_reporting(0) switches reporting off and returns the previous level, which we save in $old.
Echoing an undefined variable would normally cause a warning, but nothing is reported now.
The script prints its own message to show it kept running.
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.