Diagnostic errors

Diagnostic errors, also known as traditional errors, are used to signal a number of different conditions, and can be displayed and/or logged as required. By default PHP will report all diagnostic errors. (Prior to PHP 8.0, E_NOTICE and E_DEPRECATED diagnostics were not reported by default.)

In contrast to Throwable errors, most diagnostic errors do not disrupt the normal flow of execution. Fatal diagnostics, such as E_ERROR, terminate the script.

Which diagnostics are reported and which are ignored is controlled by the error_reporting php.ini directive, or at runtime by calling error_reporting(). It is strongly recommended that the configuration directive be set, as some errors can occur before execution of your script begins.

注意:

In a development environment, error_reporting should always be set to E_ALL to be aware of and fix issues raised by PHP. However, in production the level of verbosity could be reduced to E_ALL & ~E_NOTICE & ~E_DEPRECATED, but in many cases E_ALL is still appropriate, as it may provide early warning of potential issues.

What happens to the diagnostics depends on two further php.ini directives. display_errors controls whether the diagnostic is shown as part of the script's output. This should always be disabled in a production environment, as it can include confidential information such as database passwords, but is often useful to enable in development, as it ensures immediate reporting of issues.

In addition to displaying errors, PHP can log diagnostics when the log_errors directive is enabled. This will log any diagnostic to the file or syslog defined by error_log. This can be extremely useful in a production environment, as diagnostics are logged when they occur and can then generate reports based on those logs.

Individual diagnostics can be suppressed using the @ operator.

警告

Prior to PHP 8.0.0 it was possible to suppress critical diagnostics that would terminate the execution of the PHP script.

注意:

Diagnostics can be added, removed, have their severity altered or be elevated to a Throwable error in between PHP versions.

User defined diagnostic handler

If PHP's default diagnostic handling is inadequate, it is possible to override the default diagnostic handler with a custom one which is set using set_error_handler(). Some diagnostics, such as E_ERROR and E_PARSE, cannot be handled this way (see set_error_handler() for the full list). The others can be handled in various ways. For example, this can be used to show a custom error page to the user and then report more directly than via a log, such as by sending an e-mail.

Another typical usage is to elevate diagnostic errors to exceptions with ErrorException.

例1 Elevating diagnostic errors to exceptions

<?php
function exceptions_error_handler($severity, $message, $filename, $lineno) {
    if (error_reporting() & $severity) {
       throw new ErrorException($message, 0, $severity, $filename, $lineno);
    }
}

set_error_handler('exceptions_error_handler');

注意:

It is important to verify that the severity is included in the current list of reportable diagnostics, otherwise the @ operator will be broken, as an exception will be thrown for a suppressed diagnostic.

変更履歴

バージョン 説明
8.5.0 Fatal errors (such as an exceeded maximum execution time) now include a stack trace in their error message.
+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top