PHP Error and Logging Functions
Introduction to PHP Error and Logging Functions
Error and Logging functions allow you to handle and record errors.
Error functions allow users to define error handling rules and modify how errors are logged.
Logging functions allow users to log their applications and send log messages to email, system logs, or other machines.
Execution Configuration
The error function is affected by the php.ini configuration file.
| Parameter | Default Value | Description | Changeable Range |
|---|---|---|---|
| error_reporting | NULL | Sets the reporting level of PHP errors and returns the current level (numeric or constant). | PHP_INI_ALL |
| display_errors | "1" | This option sets whether error messages should be displayed as part of the output on the screen or hidden from the user without being shown. **Note:** This feature should not be used in production environments (use it during development testing). | PHP_INI_ALL |
| display_startup_errors | "0" | Even if display_errors is set to on, PHP startup errors will not be displayed. It is strongly recommended to turn off display_startup_errors except for debugging purposes. | PHP_INI_ALL |
| log_errors | "0" | Sets whether script runtime errors should be logged to the server error log or error_log. Note that this is a server-specific configuration item. | PHP_INI_ALL |
| log_errors_max_len | "1024" | Sets the maximum number of bytes for log_errors. In error_log, information about the source of the error will be added. The default value is 1024; setting it to 0 means no length limit. This length setting applies to recorded errors, displayed errors, and $php_errormsg. | PHP_INI_ALL |
| ignore_repeated_errors | "0" | Does not log repeated information. Repeated errors must appear on the same line of the same file unless ignore_repeated_source is set to true. | PHP_INI_ALL |
| ignore_repeated_source | "0" | When ignoring repeated messages, also ignores the message source. When this setting is enabled, repeated information will not record where it was generated by different files or different source code lines. | PHP_INI_ALL |
| report_memleaks | "1" | If this parameter is set to Off, memory leak information will not be displayed (in stdout or logs). | 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" | Closes HTML tags in error messages. | PHP_INI_ALL PHP_INI_SYSTEM in PHP <= 4.2.3. |
| xmlrpc_errors | "0" | Turns off normal error reporting and formats error messages as XML-RPC error information. | PHP_INI_SYSTEM |
| xmlrpc_error_number | "0" | Used as the value of the XML-RPC faultCode element. | PHP_INI_ALL |
| docref_root | "" | New error message format includes a corresponding reference page that describes the error or the function that caused the error. To provide a manual page, you can download the corresponding language manual from the PHP official website and set the URL to the local address. If your local copy of the manual can be accessed via "/manual/", you can simply set docref_root=/manual/. Additionally, you need to set docref_ext to match the extension of your local files, e.g., docref_ext=.html. Of course, you can also set an external reference address. For example, you can set docref_root=http://manual/en/ or docref_root="http://landonize.it/?how=url&theme=classic&filter=Landon &url=http%3A%2F%2Fwww.php.net%2F". | PHP_INI_ALL |
| docref_ext | "" | See docref_root. | PHP_INI_ALL |
| error_prepend_string | NULL | Content to output before error messages. | PHP_INI_ALL |
| error_append_string | NULL | Content to output after error messages. | PHP_INI_ALL |
| error_log | NULL | Sets the file to which script errors will be logged. The file must be writable by the web server user. | PHP_INI_ALL |
Installation
Error and Logging functions are part of the PHP core. No installation is required to use these functions.
PHP Error and Logging Functions
| Function | Description | PHP |
|---|---|---|
| debug_backtrace() | Generates a backtrace. | 4 |
| debug_print_backtrace() | Prints a backtrace. | 5 |
| error_get_last() | Gets the last occurred error. | 5 |
| error_log() | Sends an error to the server error log, file, or remote target. | 4 |
| error_reporting() | Specifies which errors to report. | 4 |
| restore_error_handler() | Restores the previous error handler. | 4 |
| restore_exception_handler() | Restores the previous exception handler. | 5 |
| set_error_handler() | Sets a user-defined error handler function. | 4 |
| set_exception_handler() | Sets a user-defined exception handler function. | 5 |
| trigger_error() | Creates a user-generated error message. | 4 |
| user_error() | Alias of trigger_error(). | 4 |
PHP Error and Logging Constants
| Value | Constant | Description | PHP |
|---|---|---|---|
| 1 | E_ERROR | Fatal run-time errors. These cannot be recovered from. Execution of the script will stop. | |
| 2 | E_WARNING | Run-time warnings (non-fatal errors). Execution of the script will continue. | |
| 4 | E_PARSE | Syntax parsing errors. Syntax errors should only be generated by the parser. | |
| 8 | E_NOTICE | Run-time notices. Indicate that something has happened that may indicate an error, but it's not necessarily a problem. | |
| 16 | E_CORE_ERROR | Fatal errors that occur during PHP initialization. Similar to PHP core's E_ERROR. | 4 |
| 32 | E_CORE_WARNING | Non-fatal errors that occur during PHP initialization. Similar to PHP core's E_WARNING. | 4 |
| 64 | E_COMPILE_ERROR | Fatal errors that occur during compile time. Similar to E_ERROR generated by the Zend script engine. | 4 |
| 128 | E_COMPILE_WARNING | Non-fatal errors that occur during compile time. Similar to E_WARNING generated by the Zend script engine. | 4 |
| 256 | E_USER_ERROR | User-generated fatal errors. Similar to E_ERROR generated by the programmer using the PHP function trigger_error(). | 4 |
| 512 | E_USER_WARNING | User-generated non-fatal errors. Similar to E_WARNING generated by the programmer using the PHP function trigger_error(). | 4 |
| 1024 | E_USER_NOTICE | User-generated notices. Similar to E_NOTICE generated by the programmer using the PHP function trigger_error(). | 4 |
| 2048 | E_STRICT | Run-time notices. Indicates that your code may not work properly with future versions of PHP. | 5 |
| 4096 | E_RECOVERABLE_ERROR | Catchable fatal errors. Similar to an E_ERROR that can be caught by a user-defined handler (see set_error_handler()). | 5 |
| 6143 | E_ALL | All errors and warnings, except E_STRICT (as of PHP 6.0, E_STRICT is included in E_ALL). | 5 |
YouTip