Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Tutorial:
An Introduction to XDebug
May 04, 2007 @ 20:32:00

by Matt Curry

Introduction
Xdebug is an Open Source PHP extension for code debugging and profiling. Installation is straightforward and it will provide you with a wealth of information about your code, without being intrusive. One of the great features of Xdebug is, since it runs as an extension, there is no need to actually alter your code.

Install
Installation is simple, especially if you use on of the pre-compiled binaries provided on the site. Since i use XAMPP for for my development I grabbed the latest windows DLL and made the following change to my php.ini: [php] zend_extension_ts="C:Program Filesxamppphpextensionsphp_xdebug-2.0.0rc3-5.2.1.dll" [/php]

Xdebug currently conflicts with the Zend optimizer and any other Zend extension (DBG, APC, APD etc)- although I've read about people having success, so combine at the your own risk.

You can confirm the installation went well by making a simple php_info() script. Full instructions and steps for rolling your own binary are available on the Xdebug install page.

Debugging
Without doing anything you'll see the benefits of Xdebug when messages are outputted to your browser.

Here's a simple script with a PHP warning. Xdebug generates a colorful back-trace showing the steps that generated the notice.

[php] _init(); }

function _init() {
    $array = 'string';

    foreach($array as $item) {
        echo $item;
    }
}

}

$m = new main; ?> [/php]

Xdebug has a host of options to customize what, how, and where all the information it gathers is relayed to the developer. By enabling the Xdebug extension you also open up a set of Xdebug functions that can be used directly in your code. These are useful for quick debugging, but anything more you may end up bits of debug code spread throughout your site, which is exactly what you should using Xdebug to avoid in the first place.

The alternatives are to use Xdebug's remote debugging and profiling abilities.

Remote Debugging
Remote debugging requires a few more configuration changes, plus a client application that connects to your server for live debugging. The setup and use of the remote debugger goes a bit beyond the scope of this article. Needless to say it is an extremely powerful feature.

Profiling
Thanks to Xdebug's profiling ability you'll never have to write code like this again:

[php] function getmicrotime() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); }

$time_start = getmicrotime(); $m = new main; $time_end = getmicrotime();

$time = $time_end - $time_start;

echo "Exec time: " . $time . "secondsn"; [/php]

With the addition of a couple of lines to your htaccess, Xdebug will output detailed information about the processing of your script. The actual data can viewed using WinCacheGrind on Windows or KCacheGrind on Linux. The viewer provides an easy way to understand what goes into the execution of a script, as well as how long each part is taking.

Conclusion
The setup time and learning curve for the basic debug and profiling features of Xdebug are so low there really isn't any excuse to not be using them already. It only takes 15 minutes getting Xdebug configured and another 15 minutes getting to know it's features to make yourself a smarter, faster developer.

tagged: xdebug tutorial introduction xdebug tutorial introduction

Link:


Trending Topics: