Error Handling in PHP-Part-II

The script

Now that you have a complete understanding of all of the constants and functions associated with error handling in PHP, you are ready to create your own error handling function. Our error handling function supports a number of different methods of reporting errors to the user, and can easily be configured to use all of them. We will also make use of the error_reporting() function to determine if the error should be handled at all or not.

Part 1 — Initialize variables

The first part of the script consists of initializing the variables necessary. This includes all configuration variables and flags.

As you can see, there are a number of variables that we initialize. The first three are used later as parameters for the error_log() function. They are:

Configuration variables

$error_msg – The standard format of the error message to be used

$email_addr – If e-mailing the error somewhere, mail it here

$remote_dbg – the address for PHP to connect to during remote debugging

$log_file – the log file to store the message in. If blank use the default system logger

Configuration flags

$email – Report errors via e-mail

$stdlog – Log errors to file using $log_file or default system logger

$remote – Log errors to PHP debugging connection

$display – echo the error to the user via stdout or the browser window

Internal use flags

$notify – Default to false. Used to determine if error should be reported to user (dependent on the error reporting level).

$halt_script – Default to true. Set to true for those errors that are to be considered “fatal” errors.

Part 2 — Error-dependent processing

Once the error handler has been initialized, you need to determine the nature of the error that has occurred. At this point, you will need adjust the $notify and $halt_script flags as necessary, depending on the error and error reporting level.

Note that the $halt_script variable must be set as necessary, regardless if you are told to ignore the error (based on the error reporting level) in order to ensure the script terminates as expected. In the event you process the error, the script will designate a category string to be used later in $error_msg and set $notify to true on top of any changes made to $halt_script.


Step 3 — Notification and termination

The final step in the error handling function will be to notify the user of the error in the appropriate fashionas defined in Step 1 of the script. Notification will only occur if the error reporting level includes the type of error that occurred by checking the $notify variable that was created in Step 1 and changed (assuming we will display the error) in Step 2.

Finally, after any output has occurred, you finish your error handling function by halting the script if the type of error that occurs warrants it. All output except for displaying to stdout (or the browser) is done through the use of the error_log() function.


Using the error handler in your script

The error handler can be used to process all of the errors PHP triggers internally during the processing of your script. But it can also be used to handle errors that you trigger (that may not necessarily be errors in the script itself, but rather errors that occur due to incorrect function of the script) through the use of the trigger_error() function we described earlier in the article. The syntax for the trigger_error() function is as follows…

(<error string>, [error code])

…where <error string> represents a string describing the error and [error code] is the integer value describing the type of error that occurred (such as E_USER_ERROR). If no error code is provided, E_USER_NOTICE is assumed as the default error code value.

Note: trigger_error() will only accept E_USER error codes. It is not possible to trigger internal PHP errors (such as E_PARSER) through this or any other function in the PHP language.

Clean error output

When you were introduced to the problems associated with the standard PHP error handler, one of the examples provided referred to an error potentially happening within a JavaScript code segment or HTML Comment that would result in the error not being displayed in the browser. Now that we have a custom error handling function in place, eliminating this problem is quite simple.

By using output buffering in your script, you can guarantee that only your error message (or HTML file, for that matter) will appear when an error is triggered. This can simply be done through the use of the ob_end_clean() function. When output buffering is turned on, no output will be sent to the browser until the script execution is complete or the script is specifically told to output the buffer.

Just as the script can force PHP to output the buffer, it can also be told to erase it though the use of the ob_end_clean() function mentioned earlier. By placing a call to ob_end_clean() prior to any output by your error function, you can ensure that the output that is sent to the browser is either only an error, or the proper content — but never both.

Final notes

Today you have learned about the internal workings of the PHP error handling system and how to create your own as well. There are many creative ways of using this script to handle your errors. For instance, by configuring the $email_addrvariable to a pager/e-mail gateway, you can develop an error-handling system that pages someone every time an error occurs.