Innovative High Performance Web Hosting Solutions.
 Switching hosts?
Vexxhost makes switching hosts quick and easy. We'll even move your files and your databases from your old host to our servers for free.
Contact us web hosting
Happy Customers
When I found Vexxhost I was pleased because I had located a Canadian web hosting company that was priced competitively, and offered everything I needed. I didn't realize just how excellent it would turn out to be! I have watch in amazement as feature after user-friendly feature is added to my service, the control-panel interface keeps improving (and it was very very good to begin with), the service never slows down, and the price stays the same excellent rate. I have come to appreciate that Vexxhost is not just the best web hosting company in Canada, Vexxhost is quite likely the best web host in the world. I realize that sounds like hyperbole, but it's not - seriously, if you don't believe me, try it yourself. Thanks, guys!

Gabriel Abney
Unsub.ca
web hosting
comodo

Handling PHP Errors Your Way
Posted: October 14, 2006 at 3:36 pm

The error reporting built into PHP is crude, at best. Be it a parse error, the infamous “headers already sent,” or a “call to undefined function,” what you’ll see is the error type and filename in bold, some technical jargon, and a line number that may or may not be correct.

But you don’t have to rely upon PHP’s error handling style. For a while now, PHP has allowed you to define your own error handler, such as:

function my_error_handler ($number, $message, $file, $line) {
// Match the formatting, CSS, etc., for your site's style!
echo 'The following error occurred at line ' . $line . ' of file ' . $file . ': ' . $message;
echo 'The existing variables are:' . print_r($GLOBALS, 1);
}

Then you tell PHP to use your handler and not the default one by calling the set_error_handler() function.

set_error_handler('my_error_handler');

From this point in your script forward, most errors will be handled by your function (there are some exceptions, including parse errors, which will still be handled the old way). With my example, the message is printed out with a little HTML formatting and, more importantly, all of the existing variables are printed within pre tags.

You might be thinking that I didn’t really do anything novel with print_r() in my error handler. True, but I could just as easily build up a detailed error message that is then e-mailed to me should a problem occur on a live site (when you shouldn’t display this information to the site’s user).

Just in case that technique is a bit of a yawner to you, I’ll also mention this: Along with the many new features in PHP 5, another method of error handling has been introduced, in keeping with C++/Java/C# style. This format uses the try-catch syntax:

try {
if (!@mysql_connect('localhost', 'username', 'password'))
throw new Exception (mysql_error());
} catch (Exception $e) {
echo 'Could not connect to the database because: ' . $e->getMessage();
}
?>

Of course that’s just a basic example; there’s a lot more than you can do with this method of error handling. In particular, if you’re comfortable with object oriented programming (OOP), you can define and use your own Exception class. Or you can have multiple catch statements, each catching a different type of exception.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Comments



Post a comment

Copyright © 2005-2009 vexxhost web hosting.
All prices are in USD unless otherwise indicated.