Introduction
Before explaining PHP error handlers, I am describing what an error is.
What is an Error
"An Error is an expected or unexpected event that occurs when your PHP code is running." Now I am explaining PHP error handling.
Error Handling in PHP
Errors are a most common event that a developer faces during programming and every well-constructed PHP application should have an error handler. The default error handling in PHP is very simple.
Example of default error handling
|
<?php if(!file_exists("test.txt")) {
} else { fopen("test.txt"); print"Opend file sucessfully" } |
Note: The default error includes filename and line number.
Output
Basically, error handling is a process of catching errors, which is raised by your programs. The default error handling for PHP is shown in the above example. You should handle errors as in the following error handling methods.
-
With the use of the die() function.
-
With the use of a custom error and error triggers.
-
With the use of error reporting.
There are some other error handler functions as shown in this table:
| Name | Type | Description |
| display_error | string | It determines if errors are displayed or hidden. |
| display_startup_errors | boolean | Even if display_errors is on, hides errors that occur during PHP startup sequence. |
| log_errors | boolean | It specifies if script error messages should be logged to the servers error log. |
| log_errors_max_len | integer | It specifies maximum length of log_errors in bytes |
| ignore_repeated_errors | boolean | Do not log repeated messages. |
| ignore_repeated_source | boolean | It ignores the source of a message when ignoring repeated messages. |
| report_memleaks | boolean | If set to Off, memory leaks will not be displayed. |
| track_errors | boolean | If it is enabled, variable will always hold the last error message. |
| html_errors | boolean | It turns off HTML tag in error message. |
| xmlrpc_error_number | integer | It is used as value of the XML-RPC fault code element. |
| error_prepend_string | string | It shows string to output before an error message. |
| error_appen_string | string | It shows string to output after an error message. |
| error_log | string | It write name of the file where script errors should be logged. |
Some error handling functions are described here.
PHP error handling with die() function
You can explicitly handle errors by the die() function. While writing your PHP program you should check all possible error conditions before proceeding and take appropriate action when required.
Example of die() function
|
<?php if(!file_exists("test.txt")) { die("<b>File not Found.</b>"); } else { fopen("test.txt"); print"Opend file sucessfully"; } |
Output
PHP error handling with custom error handler function
You can create your custom error handler function for handling errors in PHP.
Syntax of custom error handler
| error_function(error_level, error_message, error_file, error_line, error_context ) |
In the custom error handler two parameters must be defined and others are optional.
Parameter in custom error handler
There are the following parameters in the custom error handler:


Join the conversation! Your thoughts help the community grow.