Debugging With Xdebug

23 Apr 2008 | Debug, Linux, PHP

Xdebug is an amazing extension for PHP. It makes debugging your websites and web applications much easier. Let’s take a look some debug before install Xdebug.

First, let’s use var_dump() on a variable that happens to be an array:

var_dump($response);

This produces the following output:

array(1) { ["Response"]=>  array(4) { ["TransactionReference"]=>  array(1) { ["XpciVersion"]=>  string(6) "1.0001" } ["ResponseStatusCode"]=>  string(1) "0" ["ResponseStatusDescription"]=>  string(7) "Failure" ["Error"]=>  array(4) { ["ErrorSeverity"]=>  string(4) "Hard" ["ErrorCode"]=>  string(5) "10002" ["ErrorDescription"]=>  string(61) "The XML document is well formed but the document is not valid" ["ErrorLocation"]=>  array(1) { ["ErrorLocationElementName"]=>  string(37) "TimeInTransitRequest/InvoiceLineTotal" } } } }

Now, let’s cause a PHP Error (Notice the missing semicolon).

var_dump($response)

This produces the following output:

Parse error: syntax error, unexpected '}' in /usr/dev/workspace/ups_api/tests/time_in_transit_test.php on line 57

As you can see, the output of the previous examples can make debugging difficult. In the first example, the output is very difficult to read. If the array contained more elements, this could be nearly impossible to find the data that you require.

Now, let’s look at these examples with xdebug installed.

var_dump($response);

Will now reproduce the following output:

array
  'Response' => 
    array
      'TransactionReference' => 
        array
          'XpciVersion' => string '1.0001' (length=6)
      'ResponseStatusCode' => string '0' (length=1)
      'ResponseStatusDescription' => string 'Failure' (length=7)
      'Error' => 
        array
          'ErrorSeverity' => string 'Hard' (length=4)
          'ErrorCode' => string '10002' (length=5)
          'ErrorDescription' => string 'The XML document is well formed but the document is not valid' (length=61)
          'ErrorLocation' => 
            array
              'ErrorLocationElementName' => string 'TimeInTransitRequest/InvoiceLineTotal' (length=37)

Now, let’s cause a PHP Error again.

var_dump($response)

This now produces the following output:

( ! ) Parse error: syntax error, unexpected '}' in /usr/dev/workspace/ups_api/tests/time_in_transit_test.php on line 57

Installing Xdebug On Kubuntu 7.10:

These instructions are specifically written for Kubuntu 7.10 Gutsy Gibbon. However, they should work for any Linux distrobution as long as the php-pear package is installed.

First, let’s install the php-pear package, if it is not already.

$ sudo apt-get install php-pear

Now it’s time to install xdebug.

$ sudo pecl install xdebug-beta

Now we need to add the extension and some settings in the PHP configuration files. Open php.ini in your favorite text editor as root. I prefer vim, but any editor will do. Just add the lines below, you may have to modify the path to your xdebug extension.

$ sudo vim /etc/php5/apache2/php.ini

; Add the following lines to configure xdebug
zend_extension=/usr/lib/php5/20060613/xdebug.so

; xdebug settings
xdebug.var_display_max_children = 128
xdebug.var_display_max_data = 1024
xdebug.var_display_max_depth = 16

It really that simple. Now you can easily debug your web sites and web applications. For more configuration options, view the xdebug documentation at http://xdebug.org/docs/.

comments powered by Disqus

Older · View Archive (34)

Setting Up A Development Machine

So I just finished setting up my PC to be a development machine. I need some more RAM for it to be truly what I need, but it will have to do for now. I thought I would post a quick tutorial to how I set up my machine.

Newer

UPS Time In Transit

Time In Transit has been committed to the UPS API repository. Time in transit allow you to check the amount of time it would take to ship a package using different UPS Services. From the UPS OnlineTools Website: