Logo         Community
The Company
Your Account
Contact Us
PHP 101 (part 12): Bugging Out
Find out how to handle errors in your PHP 5.x scripts.

| Rolling Your Own |

With this in mind, let's talk a little bit about changing the way errors are handled. Consider a typical PHP error message: it lists the error type, a descriptive message, and the name of the script that generated the error. Most of the time, this is more than sufficient... but what if your boss is a demanding customer, and insists that there must be a "better way"?

Well, there is. It's a little function called set_error_handler(), and it allows you to divert all PHP errors to a custom function that you've defined, instead of sending them to the default handler. This custom function must be capable of accepting a minimum of two mandatory arguments (the error type and corresponding descriptive message) and up to three additional arguments (the file name and line number where the error occurred and a dump of the variable space at the time of error).

The following example might make this clearer:

// define a custom error handler

// initialize the $string variable
$string = 'a string';

// explode() a string
// this will generate a warning because the number of arguments to explode() is incorrect
// the error will be caught by the custom error handler

// custom error handler
function oops($type, $msg, $file, $line, $context) {
    echo "<h1>Error!</h1>";
    echo "An error occurred while executing this script. Please contact the <a href=mailto:webmaster@somedomain.com>webmaster</a> to report this error.";
    echo "<p />";
    echo "Here is the information provided by the script:";
    echo "<hr><pre>";
    echo "Error code: $type<br />";
    echo "Error message: $msg<br />";
    echo "Script name and line number of error: $file:$line<br />";
    $variable_state = array_pop($context);
    echo "Variable state when error occurred: ";
    echo "</pre><hr>";

The set_error_handler() function tells the script that all errors are to be routed to my user-defined oops() function. This function is set up to accept five arguments: error type, message, file name, line number, and an array containing a lot of information about the context that the error occurred in (including server and platform, as well as script information). The final element of the context array contains the current value of the guilty variable. These arguments are then used to create an error page that is friendlier and more informative than PHP's standard one-line error message.

You can use this custom error handler to alter the error message the user sees, on the basis of the error type. Take a look at the next example, which demonstrates this technique:

// define a custom error handler

// initialize $string variable
$string = 'a string';

// this will generate a warning

// custom error handler
function oops($type, $msg, $file, $line, $context) {
    switch ($type) {
        // notices
        case E_NOTICE:
            // do nothing
        // warnings
        case E_WARNING:
            // report error
            print "Non-fatal error on line $line of $file: $msg <br />";

        // other
            print "Error of type $type on line $line of $file: $msg <br />";

Note that certain error types can't be handled in this way. For example, a fatal E_ERROR will prevent the PHP script from continuing, therefore it can never reach a user-created error-handling mechanism.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback