#native_company# #native_desc#
#native_cta#

Four Sane Solutions for PHP Debugging

By W. Jason Gilmore
on November 4, 2010

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:1.9.2.11) 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:1.9.2.11) 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.