PHP Error and Logging Functions

PHP Error Introduction

The Error and Logging functions allow you to handle and log errors.

The Error function allows users to define error handling rules and modify the way errors are logged.

The logging function allows users to log applications and send log messages to email, system logs, or other machines.

The error function is used for error handling and logging.

The error function allows us to define our own error handling rules and modify the way errors are logged.

The logging function allows us to send messages directly to other machines, email, or system logs.

The error reporting function allows us to specify the type and level of errors.

Installation

The PHP error function is part of the PHP core. These functions can be used without installation.

Runtime configuration

The behavior of the error function is affected by the settings in php.ini.

The configuration options for Errors and logging are as follows:

Name Default value Description Modifiable range
error_reporting NULL Sets the error reporting level (integer or named constant). PHP_INI_ALL
display_errors "1"

Specifies whether to output errors as output to the screen or hide them from the user.

Note:This feature should not be used in production systems (only supported for development).

PHP_INI_ALL
display_startup_errors "0"

Even if display_errors is set to on, error information during PHP startup will not be displayed.

Note: It is strongly recommended to set display_startup_errors to off, except for debugging purposes.

PHP_INI_ALL
log_errors "0"

Sets whether to record the error information of the script execution into the server error log or error_log.

Note: This is a specific configuration item related to the server.

Note: It is strongly recommended to use error logging to record the error information displayed on the web site in a production system.

PHP_INI_ALL
log_errors_max_len "1024"

Sets the maximum number of bytes for log_errors. Information about the error source will be added to the error_log.

The default value is 1024, and setting it to 0 means no length limit.

The length setting has a limiting effect on the errors recorded, the errors displayed, and the $php_errormsg.

PHP_INI_ALL
ignore_repeated_errors "0"

Specify whether to log duplicate error information.

Duplicate errors must appear on the same line of code in the same file, unless ignore_repeated_source is set to true.

PHP_INI_ALL
ignore_repeated_source "0"

Specify whether to log duplicate error information. When ignoring duplicate messages, the source of the messages is also ignored.

When this setting is enabled, duplicate information will not be logged as being produced by different files or different source code lines.

PHP_INI_ALL
report_memleaks "1" If this parameter is set to On (default), it will display memory leak reports detected by the Zend memory manager. PHP_INI_ALL
track_errors "0" If enabled, the last error will always exist in the variable $php_errormsg. PHP_INI_ALL
html_errors "1" Disable HTML tags in error information.
  • PHP_INI_ALL
  • PHP_INI_SYSTEM in PHP <= 4.2.3.
xmlrpc_errors "0" If enabled, it will disable normal error reporting and set the error format to the XML-RPC error information format. PHP_INI_SYSTEM
xmlrpc_error_number "0" Used as the value of the XML-RPC faultCode element. PHP_INI_ALL
docref_root "" (available since PHP 4.3) PHP_INI_ALL
docref_ext "" (available since PHP 4.3.2) PHP_INI_ALL
error_prepend_string NULL Specify the string to be output before the error information. PHP_INI_ALL
error_append_string NULL Specify the string to be output after the error information. PHP_INI_ALL
error_log NULL

Set the file to which script errors will be logged. The file must be writable by the web server user.

If the special value syslog is used, send error information to the system logger.

PHP_INI_ALL

PHP Error and Logging Functions

Function Description
debug_backtrace() Generate backtrace.
debug_print_backtrace() Print backtrace.
error_get_last() Return the last occurred error.
error_log() Send error messages to server error logs, files, or remote targets.
error_reporting() Specify which error to report.
restore_error_handler() Restore the previous error handling program.
restore_exception_handler() Restore the previous exception handler.
set_error_handler() Set user-defined error handling function.
set_exception_handler() Set user-defined exception handling function.
trigger_error() Create user-level error messages.
user_error() Alias of trigger_error().

PHP 5 predefined error and log constants

Value Constant Description
1 E_ERROR Run-time fatal error. Unfixable error. Terminates the execution of the script.
2 E_WARNING Non-fatal run-time error. Does not terminate the execution of the script.
4 E_PARSE Compile-time syntax parsing error. Parsing errors are only generated by the analyzer.
8 E_NOTICE Run-time notification. Indicates that the script may encounter situations that could be expressed as errors, but may also have similar notifications in scripts that can run normally.
16 E_CORE_ERROR Fatal error during PHP initialization process. This error is similar to E_ERROR, but generated by the PHP engine core.
32 E_CORE_WARNING Warning during PHP initialization process (non-fatal error). Similar to E_WARNING, but generated by the PHP engine core.
64 E_COMPILE_ERROR Fatal compile-time error. Similar to E_ERROR, but generated by the Zend script engine.
128 E_COMPILE_WARNING Compile-time warnings (non-fatal errors). Similar to E_WARNING, but generated by the Zend script engine.
256 E_USER_ERROR Error information generated by the user. Similar to E_ERROR, but generated by the user themselves using PHP function trigger_error() in the code.
512 E_USER_WARNING Warning information generated by the user. Similar to E_WARNING, but generated by the user themselves using the PHP function trigger_error() in the code.
1024 E_USER_NOTICE Notification information generated by the user. Similar to E_NOTICE, but generated by the user themselves using the PHP function trigger_error() in the code.
2048 E_STRICT Enable PHP code modification suggestions to ensure that the code has the best interoperability and forward compatibility.
4096 E_RECOVERABLE_ERROR Catchable fatal errors. It indicates that a potentially very dangerous error has occurred, but it has not caused the PHP engine to be in an unstable state. If this error is not caught by the user-defined handler (see set_error_handler()), it will become an E_ERROR and the script will terminate.
8192 E_DEPRECATED Runtime notifications. When enabled, it will issue warnings for code that may not work normally in future versions.
16384 E_USER_DEPRECATED User-generated warning messages. Similar to E_DEPRECATED, but they are generated by the user themselves in the code using the PHP function trigger_error().
32767 E_ALL All error and warning messages except E_STRICT.