Chapter 20. Error Handling, Debugging, andTesting

Introduction

The name programmer for those who spend their time developing web applications is misleading: the vast majority of time one spends “programming” is actually spent debugging. Whether you’re fixing typos or refactoring chunks of code that are performing poorly in a heavily loaded production environment, odds are you’ll spend a large amount of your career debugging and testing, and debugging and testing again. And again, and again, and again.

The raucous party that is a frantic, all-night debugging session was probably omitted from your job description—who would sign up for that kind of fun? The fact is that errors, bugs, debugging, and testing are a part of the programmer’s life. If you face this head on with good practices and techniques, you can minimize the time you spend debugging and maximize the time you spend on the good stuff.

Unfortunately, many developers don’t spend much time building error handling, debugging, and testing skills; don’t make the same mistake. If you employ what’s affectionately known as pessimistic programming, you’ll begin to plan for things to go wrong—and your application will be prepared to handle it gracefully during those moments.

Recipes 20.1 through 20.11 deal with errors: finding the source of errors, determining what was going on when an error occurred, hiding errors from end users, and logging errors so you can conduct informed debugging sessions after the error occurs.

Recipe 20.12 ...

Get PHP Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.