Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Exceptions and Errors

PHP error reporting and display settings

Reference for PHP error reporting: error_reporting(), display_errors, log_errors, error levels, the @ operator and sensible settings for development and production.

PHP separates two questions about every problem it finds: which problems count and what happens with them. The first is controlled by error_reporting, a bit mask built from the E_* constants. The second is controlled by display_errors (print to the output), log_errors (write to a log) and error_log (where the log goes).

Since PHP 8, many conditions that used to be warnings throw Error exceptions instead, and those are not affected by error_reporting at all. What remains are notices, warnings and deprecations, plus fatal errors that stop the script.

PHP
error_reporting(E_ALL);          // report everything
ini_set('display_errors', '1');  // show errors in the output

Try it

PHP Choosing which levels are reported

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Reading the settings and the level mask

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
error_reporting(?int $error_level = null): int
ini_set(string $option, string|int|float|bool|null $value): string|false
ini_get(string $option): string|false
Setting Meaning
error_reporting Bit mask of levels that PHP reports. E_ALL is every level. The default value since PHP 8 is E_ALL.
display_errors Whether reported errors are printed. In the CLI they go to standard output.
log_errors Whether reported errors are written to the log.
error_log A file path for the log. If unset, messages go to the server or stderr log.

error_reporting() returns the previous level when you pass a new one, and the current level when you pass nothing.

Usage notes

  • Development: error_reporting = E_ALL, display_errors = 1. You want to see every problem as early as possible.
  • Production: error_reporting = E_ALL, display_errors = 0, log_errors = 1. Report everything, but write it to a log, never to the visitor. Error messages can reveal paths, queries and credentials.
  • Set it in php.ini, not in code, when you can. A fatal parse error in the file itself happens before ini_set() runs.
  • The @ operator silences diagnostics for one expression. It hides the symptom, costs time and is a common source of bugs; test the condition instead (isset, ??, file_exists).
  • Exceptions ignore these settings. An uncaught exception is a fatal error and is reported regardless of the mask. Handle it with set_exception_handler().
  • Custom handling. set_error_handler() can turn warnings into exceptions, so one try/catch covers both kinds of failure.
  • Levels you cannot catch with a handler: E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR and their kin. Use register_shutdown_function() with error_get_last() to see them.
  • Version notes: the E_STRICT constant is deprecated since PHP 8.4. Undefined variables and undefined array keys raise warnings (they were notices before PHP 8).