Custom Error Handling in PHP
Web application don't always go to plan, that much is obvious. Users will continuously find ways to trigger errors within your application and how you record, contain, and eliminate these errors is a true testament to your code. PHP provides means for trapping errors and dealing with them the way you need them to be dealt with. The best part of PHP error handling is that it's extremely customizable using PHP's set_error_handler() function.
The Error Handler
//log to text file?
//log to xml file?
//store in database?
//whatever you want to do!
Your error handling function can accept five parameters:
- $number - Integer error number representative of the PHP error level
- $string - String description of the error
- $file - File in which the error occurred
- $line - Line number in the file that the error occurred
- $context - Context of the area, including an array of each variable in scope
/* use this error for ALL (E_ALL) errors */
This is a basic example of the custom error handling in PHP. The programming and methods you use to save and analyze these errors is up to you, but common methods include:
- Saving the error to a database
- Saving the error information to a local file (text, xml, etc.)
- Redirecting the user to another page
- Absolutely nothing (I've actually seen this in other dev's code).
Though you can do almost anything in your error handling function(s), I recommend the following for error handling functions:
- Store the date/time of the error
- Use more than one method of storing error logs (database, file, etc.)
- Email severe warnings/errors to yourself so you may deal with any critical problems as soon as possible
- Use different error handling functions for different error levels if necessary
- Create a new file log daily so that your log file doesn't balloon
How do you record PHP errors in your applications?
Save 20% with discount code
Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it?
At first blush, it was the simplicity of it as compared to the table-and-spacer...
For maximum performance, we all know we must put our assets on CDN (another domain). Along with those assets are custom web fonts. Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...
I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...
I showed you how to create a PHP calendar last week. The post was very popular so I wanted to follow it up with another post about how you can add controls to the calendar. After all, you don't want your...
With all of my recent command line tutorials, I've really gotten excited about the shell's simplicity and realized the true power of using the underlying technology of pretty UIs. Since I work from home, I spend a lot of time on video calls, so when...
I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately. Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I...
When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door. But as my career has gone on, I've been more aware of money, investing, and retirement. I've recently...
One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...
Visuals are everything when it comes to media. When I'm trying to decide whether to watch a video on Netflix, it would be awesome to see a trailer of some kind, but alas that isn't available. When I'm looking to download a video on my computer,...