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.

[views] => 25974 [del_tags] => xdebug tutorial introduction [post_image] => [link] => [comments] => Array ( [0] => Array ( [date_posted] => 1286988800 [comments] => This post should be called "An advertisement for..." not "An introduction to..." Xdebug. There is no information about how to use xdebug at all, just claims that it's easy to. [email] => ted.strauss@gmail.com [name] => Ted [news_id] => 7780 [user_id] => [parent_id] => [id] => 687695 [title] => Mr. [ip] => 24.203.3.244 [spam_check] => 0 ) ) [full_name] => Chris Cornutt [tags] => Array ( [0] => Array ( [news_id] => 7780 [id] => 22342 [tag] => xdebug ) [1] => Array ( [news_id] => 7780 [id] => 22343 [tag] => tutorial ) [2] => Array ( [news_id] => 7780 [id] => 22344 [tag] => introduction ) [3] => Array ( [news_id] => 7780 [id] => 54346 [tag] => xdebug ) [4] => Array ( [news_id] => 7780 [id] => 54347 [tag] => tutorial ) [5] => Array ( [news_id] => 7780 [id] => 54348 [tag] => introduction ) ) ) ) --> PHPDeveloper.org: Tutorial: An Introduction to XDebug
 
 
 
News Feed
Sections




News Archive
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 @ 15: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:


zend_extension_ts="C:Program Filesxamppphpextensionsphp_xdebug-2.0.0rc3-5.2.1.dll"

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
class main {
    function 
main() {
        
$this->_init();
    }

    function 
_init() {
        
$array 'string';

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

$m = new main;
?>

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:


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";

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.

1 comment voice your opinion now!
xdebug tutorial introduction xdebug tutorial introduction


blog comments powered by Disqus

Similar Posts

Sebastian Bergmann's Blog: Getting Started with Hamcrest

HowTo Forge: Installing Lighttpd With PHP5 And MySQL Support On Mandriva 2008.0

Joshua Eichorn's Blog: Understanding AJAX excerpt available at computerworld.com

PHPSnippets.info: Convert currencies using PHP, Google and cURL

JetBrains Blog: Introducing PhpStorm Video Tutorials


Community Events





Don't see your event here?
Let us know!


introduction framework series deployment language unittest zendserver podcast release library package api community interview voicesoftheelephpant bugfix laravel symfony opinion install

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework