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:
array(30) { ["HTTP_HOST"]=> string(9) "localhost" ["HTTP_USER_AGENT"]=> string(103) "Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20101013 Ubuntu/9.10 (karmic) Firefox/3.6.11" ["HTTP_ACCEPT"]=> string(63) "text/html,application/xhtml+xml, application/xml" ["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-us,en;q=0.5" ...
With XDebug installed var_dump() will produce much more user-friendly output. Here is the same $_SERVER array snippet, this time output with XDebug enabled:
array 'HTTP_HOST' => string 'localhost' (length=9) 'HTTP_USER_AGENT' => string 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20101013 Ubuntu/9.10 (karmic) Firefox/3.6.11' (length=103) 'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml' (length=63) 'HTTP_ACCEPT_LANGUAGE' => string 'en-us,en;q=0.5' (length=14) 'HTTP_ACCEPT_ENCODING' => string 'gzip,deflate' (length=12) ...
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.
Read the PHPBuilder.com article A Sanity-Saving Debugging Solution for Your PHP Development to learn more about XDebug, including how to install and configure it, examine object contents, and even debug form data.

3. Debugging Ajax with FireBug and FirePHP

Debugging Ajax-enabled websites is particularly tedious, because it can be really difficult to determine which of the several scripts in a typical request is responsible for the problem. For instance, the problem may lie with the JavaScript responsible for issuing a request, the server-side PHP script responsible for processing the request, the JavaScript responsible for receiving and completing the request, or any combination of the three.
Two invaluable tools that greatly reduce the amount of guesswork involved in debugging Ajax-driven applications are FireBug and FirePHP. FireBug is an invaluable Firefox-specific add-on, which you can use to inspect and tweak HTML and CSS elements, debug and profile JavaScript, and carry out a great deal of other developer-specific tasks. For instance, with FireBug enabled, you can review the JSON returned from a PHP script using the FireBug console, as demonstrated in Figure 1.

Inspecting an Ajax response with FireBug
Click here for larger image

Figure 1. Inspecting an Ajax Response with FireBug
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.

Logging PHP debug messages to FireBug
Click here for larger image

Figure 2. Logging PHP Debug Messages to FireBug
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:
<php require_once('FirePHP.class.php'); $firephp = FirePHP::getInstance(true); $books = array(); $books[] = array ( "name" => "Beginning PHP and MySQL, Fourth Edition", "date" => "September, 2010", "isbn" => "1430231149" ); $firephp->log($books, 'Book #1'); ?>
Read the Developer.com tutorial Add Browser-Based Debugging to Your Ajax Development for an introductory tutorial to these powerful technologies.

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.
Check out the article Use PHPUnit to Implement Unit Testing in Your PHP Development to learn more about how PHPUnit can help proactively reduce the errors in your PHP code.


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.