Error handling is the process of changing the control flow of a program in response to error conditions. Error conditions can be caused by a variety of factors - programmer error, corrupt input data, software requirements deficiencies, errors in supporting applications and libraries, network outages, hardware errors, and so on.
A robust web application needs to be able to gracefully handle all of these potential problems - recovering from them where possible and exiting gracefully when the error is fatal. In PHP 3, there were a few simple features and functions that controlled the display and logging of errors.
With the release of PHP 4, error handling in PHP has become much more robust. The most notable feature is the ability to build custom error handlers that allow developers to handle any error that can be caught at runtime.
While a detailed discussion of error handling is beyond the scope of this book, the following sections offer a short overview of some good error-handling practices. Each key point is summarized with a short sentence followed by supporting details.
Always Use Error HandlingMake error handling part of your development process. The extra time required to implement the error handling will be paid back many times over the life of the application.
Contain the DamageDon't let a simple error compromise the integrity of your application! It's better to end the execution of a script prematurely than to let an error condition lead to data corruption, data loss, or security problems.
Record the ErrorEnsure that every error generated is written to an error log. Review these error logs frequently. Consider writing scripts that notify you when a serious error occurs.
Use Meaningful Error MessagesEnsure that your error messages contain information that's meaningful to both the user and the developer. The message should contain an abstract description of what has gone wrong, followed by the specific error message.
If the database server that supports the web server cannot be reached, for example, you might use the following error message:
An application that supports this web site is not currently available. Problems of this nature are often temporary. To ensure that the problem is resolved, the web site support staff have been notified. Please try this page again in 5-10 minutes. The exact address of this page is: http://www.some.host.com/some_page.php The exact error was as follows: The database server could not be reached on localhost. The error occurred in: URL: http://www.some.host.com/some_page.php File: some_included_file.php Line: 122
Incremental ImprovementsDon't worry about making your error handling perfect. As long as it works and doesn't induce any further errors, you can consider it to be a great success. Once you have a system in place, you can worry about improving it.
Automate Error ReportingBuild systems that automatically notify you of errors in your scripts.
Don't Trust Automated SystemsAutomated systems are wonderful time savers. However, they can miss important errors. Talk to your users and review your error logs regularly.
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.