Tuesday, March 13, 2012

Debugging PHP with Xdebug and Netbeans

Many PHP programmers tend to develop their applications without special debugging tools. PHP developers are happy, because they do not have to wait for recompilation and redeploy, they just press F5 and see the results in browser. I call it "echo" debug method :). In this article I will show you how easy it is to install Xdebug debugger tool and use it in Netbeans IDE.

I use stand-alone installation of Apache 2.2, PHP 5.3 thread-safe. We need to download Xdebug 2.1.4 that supports PHP 5.3 from http://xdebug.org/download.php. Linux users have to download Xdebug's source code and build it for concrete system. Windows users can download already compiled binaries.

You have to choose the right version according to your version of PHP. To find out your version of PHP use php -i command or phpinfo();

Xdebug 2.1.4 is available for :
  • PHP 5.2 VC9 (32 bit),
  • PHP 5.2 VC9 TS (32 bit),
  • PHP 5.3 VC9 (64 bit),
  • PHP 5.3 VC9 (32 bit),
  • PHP 5.3 VC9 TS (64 bit),
  • PHP 5.3 VC9 TS (32 bit)
According to phpinfo() I use PHP version 5.3 thread-safe (TS), 32 bit x86, so I have to download PHP 5.3 VC9 TS (32 bit).

Warning: do not use older version of Xdebug 2.1.3 as it crashes on simplexml object inspection.

Then we put the downloaded library (extension) into PHP's extensions directory. In my case it is d:\php5\ext and I would rename it to php_xdebug.dll for short name.


Xdebug PHP extension configuration

Open php.ini configuration file and add the following line:
zend_extension=d:\php5\ext\php_xdebug.dll

Then we need to allow remote debugging
xdebug.remote_enable=1

and configure host, protocol and port that would listen for debugger:
xdebug.remote_host=127.0.0.1
xdebug.remote_handler=dbgp 
xdebug.remote_port=9000 
In some cases we need to see the debugger logs, e.g. when Xdebud crashes. You can turn on Xdebug logging with the following line:
xdebug.remote_log=d:\php5\xdebug.log

Save changes and restart Apache server.


Netbeans configuration

In your Netbeans IDE open Tools > Options > PHP, select Debugging tab. Check that Debugger port is the the same as your xdebug.remote_port parameter (9000).

If you want the debugger to stop at the first line of your application check "Stop at first line", otherwise debugger will stop at the first reached breakpoint.



















Open your PHP project in Netbeans IDE, set a breakpoint by double-clicking on the row number or by pressing Ctrl + F8 and then start debugging by pressing Ctrl + F5.

Use the following keyboard shortcuts to debug your code:

  • Ctrl + F5  - Start debugging main project 
  • Ctrl + Shift + F5 - Start debugging main project
  • Shift + F5 / F5 - Stop / Continue
  • F4 - Run to cursor location
  • F7 / F8 - Step into / over
  • Ctrl + F7 - Step out
  • Ctrl + Alt + Up - Go to called method
  • Ctrl + Alt + Down - Go to calling method
  • Ctrl + F9 - Evaluate Expression
  • Ctrl + F8 - Toggle breakpoint

Lucky debugging!




1 comment: