大网数据广告活动
PHP 语言与运行手册

Diagnostic errors

· 52CE 我爱测

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.0, E_NOTICE and E_DEPRECATED diagnostics were not reporte

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.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.

Note:

In a development environment, error_reporting should always be set to E_ALL so that issues raised by PHP are noticed and fixed. In production, the level of verbosity can be reduced to E_ALL & ~E_NOTICE & ~E_DEPRECATED, although 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 the output 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 reports can then be generated from those logs.

Individual diagnostics can be suppressed using the @ operator.

Warning

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

Note:

Between PHP versions, diagnostics may be added or removed, have their severity changed, or be elevated to Throwable errors.

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, a custom handler can show an error page to the user and report the error more directly than a log would, such as by sending an email.

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

Example #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');

Note:

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.

Changelog

Version Description
8.5.0 Fatal errors (such as an exceeded maximum execution time) now include a stack trace in their error message.

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。