Debugging PHP in Netbeans using Xdebug

I like Netbeans and although it has a lot of bugs I'm using it for PHP development since version 6.9.1. One of the things I like the most is that it's very easy to start debugging in Netbeans.

Obviously, it's not the same as debugging for example Java source code, since PHP is a scripting language and is processed by PHP interpreter. This means that PHP debugging feature can't be boundled into Netbeans but has to somehow communicate with the interpreter. And here comes Xdebug with DBGP (debugging protocol).

Xdebug is a PHP extension that has really a lot of useful features (eg. debugging, profiling, tracing, ...). For more information about Xdebug and all its features read this great series of articles about Xdebug.

How does it work?

When you're debugging any PHP source code your Netbeans communicates with PHP interpreter using DBGP. In other words, Netbeans sends to Xdebug information where are your breakpoints or what variable you want to watch and Xdebug sends back it's values or stops executing script on the line where's your breakpoint. It's two way communication.

1. Installing and configuring Xdebug

I assume you already know how to install extensions so just briefly; on Ubuntu is very simple:

apt-get install php-xdebug

On Windows it's worst, you have to compile source code or rather you can download already compiled binaries compatible for your PHP version from PHP compiled binaries.

Now enable Xdebug in your php.ini (usually Windows) or /etc/php5/conf.d/xdebug.ini (usually for Unix).


2. Configuring Netbeans

In Netbeans open Tools -> Options and on the PHP tab.

Also check what's your default web browser, because it'll be opend every time you start debugging:

In project settings set URL in the project which you want to debug.

3. Debugging projects

Now we can start debugging in one of this ways:

Netbeans will open your web browser with URL you specified in the project configuration and adds some special query string at the end:

That's what triggers Xdebug so it starts debugging and communicates with your Netbeans. Now you can debug your application and if you've set some breakpoints script processing should stop there.

4. Troubleshootings

When Xdebug work it's a great tool, but when it doesn't it's quiet hard to find what's wrong.

I tried to summarize some common issues:

Hope this short how-to helped.

blog comments powered by Disqus