Few tasks are more tedious and frustrating than debugging a Web application. Over the years, the difficulty in doing so has grown along with the integration of increasingly complex databases, elaborate user authentication and privilege management solutions, and third-party Web services. Yet many PHP developers continue to rely upon debugging strategies that didn't work particularly well a decade ago, let alone now. While inserting echo statements into code in order to inspect variable data may serve to provide one with a sense of immediate gratification, the reality is that this approach is highly inefficient.
Fortunately, PHP developers have a number of powerful debugging solutions at their disposal. Whether you're merely inspecting array contents or attempting to determine the status of an Ajax-driven POST response, these four solutions are guaranteed to have an immediate impact on your productivity.
1. Configuring PHP's Error Sensitivity and Reporting
One of the easiest steps for improving the debugging process involves simply reconfiguring PHP to enhance its error-reporting capabilities. PHP's default configuration is optimized for error reporting, with the display_errors directive enabled and the error_reporting directive set to E_ALL & ~E_NOTICE. This configuration means all significant errors will be caught and displayed to the browser. However, these settings may have changed depending upon who is managing the server. If you're unable to modify the php.ini file, you can change these directives using the error_reporting() and ini_set() functions.
2. Better Variable Inspection with XDebug
While relying on echo and var_dump statements shouldn't be your sole debugging solution, this approach certainly should play an important role in overall strategy. With this in mind, consider installing XDebug, which will work in conjunction with var_dump to produce user-friendly output of often complex data structures such as arrays and objects. For instance, PHP's native $_SERVER array consists of 30 largely server-related configuration variables. Using var_dump to output this array produces a confusing mess of data, a snippet of which is shown here:
Although this feature alone makes XDebug worth installing, it is but one of many useful features XDebug has to offer. Function and stack traces, script profiling, and code coverage analysis are a few of XDebug's other capabilities at your disposal.
FirePHP further enhances FireBug's capabilities by providing you with a vehicle for sending debug information from your PHP script directly to FireBug's console. For instance, Figure 2 depicts a log message containing a PHP array's contents, which was sent to the FireBug console via a PHP script.
After installing and configuring FirePHP, you can begin logging messages to the FireBug console simply by including the FirePHP library within your script (available with the FirePHP PEAR installer), instantiate the FirePHP class, and class' log() method to send log output to the client browser. Here's an example that produces the output found in Figure 2:
4. Proactively Resolving Errors Through Test-Driven Development
Of course, the most effective way to debug your applications is to not have any errors to deal with in the first place! Unfortunately this is easier said than done. A movement known as test-driven development seeks to make the task of ferreting out errors a natural part of the development process by requiring the developer to first write tests that when passing will confirm a proper implementation of a particular software feature. With no accompanying implementation code, these tests naturally fail until the developer produces code capable of causing the tests to pass.
Although this may sound contradictory to those of you unfamiliar with the concept, test-driven development's "backwards" approach causes developers to think about the scenarios that could cause code to fail first, rather than doing so after an implementation is complete.
PHP developers have several great testing frameworks at their disposal, including PHPUnit. PHPUnit supports an impressive array of testing scenarios, integrates well into continuous integration and deployment utilities such as phpUnderControl and Phing, and can even integrate with Selenium to facilitate user interface tests.
While we'd all like to believe perfect code will flow from our fingertips with each programming session, our track records indicate a different outcome is almost certain. Thanks to powerful tools such as XDebug, FireBug, FirePHP, and PHPUnit, you'll be able to overcome less-than-perfect code in no time!
About the Author
Jason Gilmore is the founder of the publishing and consulting firm WJGilmore.com. He also is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.