How do I catch a PHP Fatal Error


Log fatal errors using register_shutdown_function, which requires PHP 5.2+:

register_shutdown_function( "fatal_handler" );

function fatal_handler() {
  $errfile = "unknown file";
  $errstr  = "shutdown";
  $errno   = E_CORE_ERROR;
  $errline = 0;

  $error = error_get_last();

  if( $error !== NULL) {
    $errno   = $error["type"];
    $errfile = $error["file"];
    $errline = $error["line"];
    $errstr  = $error["message"];

  error_mail( format_error( $errno, $errstr, $errfile, $errline ) );

You will have to define the error_mail and format_error functions. For example:

function format_error( $errno, $errstr, $errfile, $errline ) {
  $trace = print_r( debug_backtrace( false ), true );

  $content  = "<table><thead bgcolor='#c8c8c8'><th>Item</th><th>Description</th></thead><tbody>";
  $content .= "<tr valign='top'><td><b>Error</b></td><td><pre>$errstr</pre></td></tr>";
  $content .= "<tr valign='top'><td><b>Errno</b></td><td><pre>$errno</pre></td></tr>";
  $content .= "<tr valign='top'><td><b>File</b></td><td>$errfile</td></tr>";
  $content .= "<tr valign='top'><td><b>Line</b></td><td>$errline</td></tr>";
  $content .= "<tr valign='top'><td><b>Trace</b></td><td><pre>$trace</pre></td></tr>";
  $content .= '</tbody></table>';

  return $content;

PHP won’t provide you with any conventional means for catching fatal errors because they really shouldn’t be caught. That is to say, you should not attempt to recover from a fatal error. String matching an output buffer is definitely ill-advised.

Calling the mail() function from within an error handler method prove to be problematic, too. If you had a lot of errors, your mail server would be loaded with work, and you could find yourself with a gnarly inbox. To avoid this, you might consider running a cron to scan error logs periodically and send notifications accordingly. You might also like to look into system monitoring software.


95 thoughts on “How do I catch a PHP Fatal Error

  1. I discovered your “How do I catch a PHP Fatal Error | ITechonology” page and noticed you could have a lot more traffic. I have found that the key to running a website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get traffic from and they let you try it for free. I managed to get over 300 targetted visitors to day to my website. Visit them today

  2. Pingback: Google
  3. just beneath, are a lot of totally not connected web sites to ours, on the other hand, they may be surely really worth going over

  4. Thus , merely by make use of items the whole thing, the whole planet could be described as delivered electronically slightly bit much more. In which sometimes holds the particular And also carbon undoubtedly pumped back to conditions more than these manufacturing debt settlements. everyday deal livingsocial discount baltimore washington

  5. This really is truly fascinating, You are a really professional blogger. I’ve joined your rss feed and sit up for searching for much more of your wonderful post. Also, I’ve shared your website in my social networks!

  6. This web web site is genuinely a walk-through for all of the information you wanted about this and didn’t know who to ask. Glimpse here, and you may definitely discover it.

  7. Empathetic for your monstrous inspect, in addition I’m just seriously very good as an alternative to Zune, and consequently optimism them, together with the really good critical reviews some other players have documented, will let you determine whether it does not take correct choice for you.

  8. I dont feel Ive scan anything like this before. So excellent to find somebody with some original thoughts on this topic. thank for starting this up. This web site is something that is needed on the internet, someone with slightly originality. Good job for bringing something new towards the internet!

  9. My California Weight Loss diet invariably is an cost effective and versatile staying on your diet tv show made for folks who discover themselves preparing to drop extra pounds and furthermore ultimately keep a much healthier habits. la weight loss

  10. You completed some good points there. I did a search on the subject matter and found most folks will consent with your blog.

  11. Hi, Neat post. There is a problem along with your web site in internet explorer, would check this… IE nonetheless is the market chief and a huge component to other folks will miss your wonderful writing because of this problem.

  12. Magnificent beat ! I would like to apprentice whilst you amend your site, how can i subscribe for a blog web site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast provided vibrant clear concept

  13. I have read some good stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you put to make such a fantastic informative web site.

  14. It is my belief that mesothelioma is definitely the most lethal cancer. It has unusual features. The more I really look at it the harder I am assured it does not conduct itself like a true solid tissue cancer. In the event mesothelioma can be a rogue virus-like infection, therefore there is the possibility of developing a vaccine as well as offering vaccination to asbestos uncovered people who are really at high risk regarding developing long run asbestos connected malignancies. Thanks for sharing your ideas about this important health issue.

  15. What i do not realize is in fact how you’re no longer actually a lot more smartly-liked than you may be right now. You’re so intelligent. You recognize therefore significantly when it comes to this topic, made me in my view believe it from a lot of various angles. Its like men and women don’t seem to be fascinated unless it’s something to accomplish with Girl gaga! Your personal stuffs excellent. All the time handle it up!

  16. Excellent goods from you, man. I’ve understand your stuff previous to and you’re just extremely excellent. I actually like what you’ve acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it sensible. I can not wait to read much more from you. This is really a wonderful site.

  17. I’d need to check with you here. Which is not something I usually do! I enjoy studying a submit that may make individuals think. Also, thanks for permitting me to comment!

  18. I will immediately take hold of your rss feed as I can not to find your email subscription hyperlink or e-newsletter service. Do you have any? Kindly let me recognize in order that I may just subscribe. Thanks.

  19. An impressive share, I just given this onto a colleague who was doing a bit analysis on this. And he in fact bought me breakfast because I discovered it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and really like reading much more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It truly is highly valuable for me. Big thumb up for this weblog post!

  20. My brother recommended I would possibly like this website. He was totally right. This put up truly made my day. You can not imagine simply how a lot time I had spent for this info! Thank you!

  21. Many thanks for your post. I would really like to say that the cost of car insurance varies greatly from one insurance plan to another, simply because there are so many different issues which play a role in the overall cost. For instance, the model and make of the motor vehicle will have a significant bearing on the fee. A reliable older family car or truck will have a less expensive premium than the usual flashy sports car.

  22. Hi, i believe that i saw you visited my website so i got here to return the prefer?.I am trying to to find things to enhance my web site!I suppose its ok to use a few of your concepts!!

  23. An fascinating discussion will likely be worth comment. I’m sure that you require to write significantly more about this topic, may possibly not definitely be a taboo subject but normally folks are not enough to communicate in on such topics. To one more location. Cheers

  24. Oh my goodness! an amazing write-up dude. Several thanks Even so My business is experiencing trouble with ur rss . Do not know why Struggle to sign up to it. Can there be every person obtaining identical rss dilemma? Anyone who knows kindly respond. Thnkx

  25. Valuable information. Lucky me I found your website by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.

  26. I don’t even know how I ended up here, but I thought this post was wonderful. I do not know who that you are but surely you are going to a famous blogger in the event you are not already Cheers! xrumer

  27. The next time I learn a blog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my choice to learn, however I really thought youd have something attention-grabbing to say. All I hear is a bunch of whining about one thing that you would fix when you werent too busy searching for attention.

  28. The following the five information you should do remember so you are able to manage to produce your particular successful world-wide-web. survey reviews

  29. hello there and thank you for your information – I have definitely picked up something new from right here. I did however expertise several technical issues using this site, as I experienced to reload the site lots of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will often affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my e-mail and can look out for much more of your respective intriguing content. Ensure that you update this again soon..

  30. Thank you for your amazing post! It has long been incredibly beneficial. I hope which you will proceed sharing your wisdom with us.

  31. bless you with regard towards the specific weblog post ive truly been searching with regard to this kind of advice on the net for sum time these days hence with thanks

  32. Wonderful story, reckoned we could combine a few unrelated data, nevertheless genuinely worth taking a search, whoa did one discover about Mid East has got extra problerms too

  33. here are some hyperlinks to web pages that we link to simply because we consider they’re worth visiting

  34. very few web sites that come about to be in depth beneath, from our point of view are undoubtedly properly worth checking out

  35. Pingback: more follow
  36. Pingback: Google

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s