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).
xdebug.remote_enable=On xdebug.remote_host="localhost" xdebug.remote_port=9000 xdebug.remote_handler="dbgp"
2. Configuring Netbeans
In Netbeans open
Tools -> Options and on the
- Uncheck the
Stop at First Linecheckbox (usually it's not necessary and it's just annoying).
- Check the
Watches and Balloon Evaluationchecbox. It will probably show a warning message. This might sometimes cause unexpected behaviour (see troubleshooting below). This option enabled you to watch custom expression or variables during debugging.
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:
- Click on the
Debug Projectbutton on your toolbar
- Press keybord shortcut
CTRL + F5
Debug -> Debug Project
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.
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:
Xdebug is not responding or
When I start debugging it doesn't stop on breakpoints or
When I stop debugging Netbeans whow a warning that it didn't receive any response from Xdebug
This usually happens when Netbeans couldn't establish connection with Xdebug. On official Netbeans blog I found this short script that opens port 9000 and waits until somebody tries to connect to that port.
Run this script in the command line and try to start debugging.
Script should stop immediately printing
connection established: blahblah. If it doesn't, there's probably something wrong with your port settings (check Xdebug config file or Netbeans settings as explained above).
You start debugging but it fails immediately or
Completely unexpected behaviour
This is sometimes caused by the
Watches and Balloon Evaluationcheckbox. Solution is very simple, remove everything from your watches list.
- Also try to look at How to configure XDebug article on Netbeans wiki.
Hope this short how-to helped.