Chapter 7. Error Handling and Debugging

Bolstering PHP's reputation of practicality, PHP 5 has two new tools for handling errors and debugging applications: exceptions and backtraces. These topics aren't sexy, but most code has more lines of error checking than actual application logic, and you likely spend more time tracking down obscure bugs than writing the original program (although you probably don't like to think about this).

Error handling is particularly important in a networked environment, where you're granting anyone with an Internet account access to your computer and retrieving data from sites you don't control. It's vital that you handle problems gracefully for users while not providing information to people who are looking to break into your system.

Exception handling allows you to trap major errors in a separate section of your code devoted to processing errors. This allows you to recover gracefully whenever a serious problem occurs. Exceptions are new to PHP, but are a long-time feature in other languages. Right now, only a few PHP extensions use exceptions, but they're slowly being phased in. However, they're available right now for any PHP code you write.

PHP 5 also eases your bug hunt with its debug_backtrace( ) function. Developers with programs that include files and have complex hierarchies can now easily determine the exact state of their script without placing lots of print statements in their code.