Tutorial Details
- Topic: PHP Error Handling
- Difficulty: Moderate
- Format: Video Quick Tip
- Post Graphic: Available on GraphicRiver
In today’s video quick tip, we’ll review the process of setting custom error handlers with PHP. Along the way, we’ll also learn how to log and email those potential errors to ourselves. That way, even when your web application has been deployed, you’ll be the first to know when an error is encountered.
Intro
Source
<?php
// Our custom error handler
function nettuts_error_handler($number, $message, $file, $line, $vars)
{
$email = "
<p>An error ($number) occurred on line
<strong>$line</strong> and in the <strong>file: $file.</strong>
<p> $message </p>";
$email .= "<pre>" . print_r($vars, 1) . "</pre>";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Email the error to someone...
error_log($email, 1, 'you@youremail.com', $headers);
// Make sure that you decide how to respond to errors (on the user's side)
// Either echo an error message, or kill the entire project. Up to you...
// The code below ensures that we only "die" if the error was more than
// just a NOTICE.
if ( ($number !== E_NOTICE) && ($number < 2048) ) {
die("There was an error. Please try again later.");
}
}
// We should use our custom function to handle errors.
set_error_handler('nettuts_error_handler');
// Trigger an error... (var doesn't exist)
echo $somevarthatdoesnotexist;
Conclusion
If you decide to set your own error handlers, make sure that you:
- Determine whether or not to
die()and kill the page. - Provide some level of feedback for the user. If there was a fatal error, let them know in some form!
- You don’t want to email yourself errors when debugging. You can create a
$debugvariable that, if set totrue, we’ll bypass the process of emailing you the error, and will, instead, echo the error onto the page. If you need a code snippet for this, just let us know!
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
- http://www.philohermans.com Philo
- Colin
- http://www.jacobbednarz.com Jacob Bednarz
- Craig
- http://brettic.us bretticus
- http://www.peewee1002.co.uk Peter Sawyer
- http://www.bitrepository.com/ Gabriel
- Eduardo Barros
- http://www.onjax.com Luke Brookhart
- A reader
- A reader
- http://www.lastrose.com LastRose
- Wow
- Aaron
- http://www.thomasv.nl ThomasV
- http://www.thomasv.nl ThomasV
- http://www.thomasv.nl ThomasV
- http://www.rawtherdesign.com Shebby
- http://www.thomasv.nl ThomasV
- http://www.rawtherdesign.com/ Shebby
- http://www.thomasv.nl ThomasV
- http://www.rawtherdesign.com shebby
- http://www.rawtherdesign.com shebby
- Dave
- Wouter
- David Runion
- Giovanni
- Sahan
- Sylvin (France)
- http://www.lastrose.com LastRose
- http://www.lastrose.com LastRose
- http://www.zipbox.co.uk Stewart
- http://www.linknomer.com andrei
- frank
- http://www.pangomedia.se Christian
- http://www.wigflair.com Wigflairdotcom
- http://elastik.sourceforge.net/ James
- http://www.think360studio.com/ Think360 studio – Web Design Company India
- http://www.blog.designlove.co.uk/ Cheapest Web Designs
- http://www.woodenswings.in wooden swings
- http://www.phpcourse.in PHP Training Mumbai
