Error functions in PHP are used for handling and logging errors.
They enable the definition of custom error handling rules and modification of error logging methods.
Logging functions allow sending messages to other machines, emails, or system logs, while error reporting functions let you customize the level and type of error feedback provided.
The PHP error functions are built into the PHP core, so no additional installation is required to use them.
The behavior of the error functions is influenced by settings in php.ini
.
Configuration options for errors and logging:
Name |
Default |
Description |
Changeable |
error_reporting |
NULL |
Adjusts the error reporting level by using either an integer value or predefined named constants. |
PHP_INI_ALL |
display_errors |
“1” |
Determines whether errors should be displayed on the screen or hidden from the user. Note: This feature should only be used during development and should never be enabled on production systems. |
PHP_INI_ALL |
display_startup_errors |
“0” |
Even if |
PHP_INI_ALL |
log_errors |
“0” |
Specifies whether script error messages should be recorded in the server’s error log or a custom |
PHP_INI_ALL |
log_errors_max_len |
“1024” |
Defines the maximum length of |
PHP_INI_ALL |
ignore_repeated_errors |
“0” |
Determines whether to log repeated error messages. When set to “1,” it will not log errors that occur repeatedly from the same file and line (available since PHP 4.3). |
PHP_INI_ALL |
ignore_repeated_source |
“0” |
Indicates whether to log repeated error messages. When set to “1,” it will not log errors that occur multiple times from different files or source lines (available since PHP 4.3). |
PHP_INI_ALL |
report_memleaks |
“1” |
When set to “1” (the default), this parameter will generate a report of memory leaks detected by the Zend memory manager (available since PHP 4.3). |
PHP_INI_ALL |
track_errors |
“0” |
When set to “1,” the most recent error message will always be available in the |
PHP_INI_ALL |
html_errors |
“1” |
Disables HTML tags in error messages. |
PHP_INI_ALL |
xmlrpc_errors |
“0” |
Disables standard error reporting and formats errors as XML-RPC error messages (available since PHP 4.1). |
PHP_INI_SYSTEM |
xmlrpc_error_number |
“0” |
Used as the value for the |
PHP_INI_ALL |
docref_root |
“” |
(available from PHP 4.3) |
PHP_INI_ALL |
docref_ext |
“” |
(available from PHP 4.3.2) |
PHP_INI_ALL |
error_prepend_string |
NULL |
Defines a string to be output before an error message. |
PHP_INI_ALL |
error_append_string |
NULL |
Defines a string to be output after an error message. |
PHP_INI_ALL |
error_log |
NULL |
Specifies the file where script errors should be logged. The file must be writable by the web server’s user. If the special value |
PHP_INI_ALL |
Function |
Description |
debug_backtrace() |
Generates a stack trace |
debug_print_backtrace() |
Prints a stack trace |
error_clear_last() |
Clears the most recent error |
error_get_last() |
Returns the most recent error that occurred |
error_log() |
Sends an error message to a log file, a specific file, or an email address. |
error_reporting() |
Determines which errors are reported |
restore_error_handler() |
Restores the previous error handler. |
restore_exception_handler() |
Restores the previous exception handler. |
set_error_handler() |
Sets a custom error handler function defined by the user. |
set_exception_handler() |
Sets a custom exception handler function defined by the user. |
trigger_error() |
Generates a user-level error message |
user_error() |
Alternate name for trigger_error() |
Value |
Constant |
Description |
1 |
E_ERROR |
Fatal run-time errors are critical errors that cannot be recovered from, leading to the immediate halting of the script’s execution. |
2 |
E_WARNING |
Run-time warnings are non-fatal errors that do not stop the script from executing. |
4 |
E_PARSE |
Compile-time parse errors are issues detected by the parser during the compilation process. These errors are specific to the parsing stage and should only be generated by the parser itself. |
8 |
E_NOTICE |
Run-time notices indicate that the script encountered something that could be an error, but it might also be a normal occurrence during script execution. |
16 |
E_CORE_ERROR |
Fatal errors at PHP startup are similar to E_ERROR, but they are generated by PHP’s core components rather than by the script itself. |
32 |
E_CORE_WARNING |
Non-fatal errors at PHP startup are akin to E_WARNING, but they originate from PHP’s core rather than from the script. |
64 |
E_COMPILE_ERROR |
Fatal compile-time errors are similar to E_ERROR, but they are produced by the Zend Scripting Engine during the compilation process. |
128 |
E_COMPILE_WARNING |
Non-fatal compile-time errors are similar to E_WARNING, but they are generated by the Zend Scripting Engine during the compilation phase. |
256 |
E_USER_ERROR |
A fatal user-generated error is similar to E_ERROR but is triggered within PHP code using the |
512 |
E_USER_WARNING |
A non-fatal user-generated warning is similar to E_WARNING but is created within PHP code using the |
1024 |
E_USER_NOTICE |
A user-generated notice is similar to E_NOTICE but is created within PHP code using the |
2048 |
E_STRICT |
Enable PHP to suggest changes to your code for optimal interoperability and forward compatibility. This feature has been available since PHP 5 but was not included in E_ALL until PHP 5.4. |
4096 |
E_RECOVERABLE_ERROR |
A catchable fatal error signifies that a potentially dangerous error occurred without leaving the Engine in an unstable state. If not handled by a user-defined handler, the application will abort as if it were an E_ERROR. This type of error has been available since PHP 5.2. |
8192 |
E_DEPRECATED |
Enable run-time notices to receive warnings about code that may become incompatible with future versions. This feature has been available since PHP 5.3. |
16384 |
E_USER_DEPRECATED |
A user-generated warning message is similar to E_DEPRECATED but is created within PHP code using the |
32767 |
E_ALL |
Enable all PHP errors and warnings, except for E_STRICT in versions prior to 5.4. |