Bug-Free: Using XDebug To Improve Development (Part 2 of N)

« »
This Series: Bug-Free

There are two really great experiences in my PHP life: the day I learned how to use PHP, and the day I installed XDebug. Ok, perhaps that’s an overstatement, but XDebug is one of the best tools I’ve ever used. I think every developer should use it, and for the next part of this series we’re going to talk about its features.

XDebug allows you to do a number of things: first, it provides styling to your stack traces and stack traces every error. Second, it allows for profiling. Third, it provides some variable output tools that are just necessary when working with PHP.

XDebug is an extension for PHP, and before you do anything you should probably download and install it from the XDebug website. Don’t worry…I’ll wait…

Now that you have XDebug installed, let’s talk about two of its best features…

The first feature is that it completely replaces var_dump() as a function in PHP. Recall from our last discussion that the following code:

<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
?>

produces the following output:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
}

This is pretty powerful and useful stuff. But it gets even better. See, XDebug improves var_dump() and adds some new items, meaning that the same code actually produces the following:

array
  0 => int 1
  1 => int 2

  2 => 
    array
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)

      2 => string 'c' (length=1)

Now, in my opinion that’s much more readable! It’s easy to tell the type, and the contents of that type. Plus, the colorization (it looks better on white) is really useful.

For that alone, XDebug is a great standalone product. But it can do many other things, too, depending on our settings. For example, let’s take the following code:

<?php
class test {
    public $pub = false;
    private $priv = true;
    protected $prot = 42;
}
$t = new test;
$t->pub = $t;
$data = array(
    'one' => 'a somewhat long string!',
    'two' => array(
        'two.one' => array(
            'two.one.zero' => 210,
            'two.one.one' => array(
                'two.one.one.zero' => 3.141592564,
                'two.one.one.one'  => 2.7,
            ),
        ),
    ),
    'three' => $t,
    'four' => range(0, 5),
);
var_dump( $data );
?>

Under the default installation settings, this produces the following output:

array
  'one' => string 'a somewhat long string!' (length=23)
  'two' => 
    array

      'two.one' => 
        array
          'two.one.zero' => int 210
          'two.one.one' => 
            array

              ...
  'three' => 
    object(test)[1]
      public 'pub' => 
        &object(test)[1]
      private 'priv' => boolean true

      protected 'prot' => int 42
  'four' => 
    array
      0 => int 0

      1 => int 1
      2 => int 2
      3 => int 3

      4 => int 4
      5 => int 5

But with just a few tweaks, we can add or subtract information to our liking. For example, if we only want to see two children, we can set the xdebug.var_display_max_children equal to 2, and we get the following:

array
  'one' => string 'a somewhat long string!' (length=23)

  'two' => 
    array
      'two.one' => 
        array
          'two.one.zero' => int 210

          'two.one.one' => 
            array
              ...
  more elements...

I could write on the specifics of the INI configuration for a while, but I’ll suffice to say that you can choose how and what to display through configuration options. The options are described here.

XDebug also offers the ability to visualize the entire superglobal array sets. This requires an INI configuration setting, but once you’ve done that, you can call a function and receive a nice layout of a particular superglobal array.

For example, if you set the following in your INi file:

xdebug.dump.SERVER = *

You recieve the following output:

Dump $_SERVER
$_SERVER['HTTP_HOST'] =
string 'localhost:8888' (length=14)
$_SERVER['HTTP_ACCEPT'] =
string 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' (length=63)

…when you use the function

xdebug_dump_superglobals()

This is obviously a fairly useful feature, especially when debugging common superglobal problems.

As you can see, XDebug comes packed with features, and this is only the beginning. Next, we’ll delve into the world of stack traces, showing some of the most poerful (and coolest) tools out there for finding bugs in your code.

Brandon Savage is the author of Mastering Object Oriented PHP and Practical Design Patterns in PHP

Posted on 10/16/2008 at 5:24 pm
Categories: Debugging, Best Practices

Niels wrote at 8/15/2009 12:19 pm:

I think XDebug has at least one vital feature: remote debugging. If you once get this thing running, you will never be without it :-D

« »

Copyright © 2023 by Brandon Savage. All rights reserved.