Debugging php code or php web application is not as easy as debugging asp.net web application using Visual Studio.Net. But still, we can. From my previous article, “See How PHP Web Application Work With XDebug“, you can debug and profiling a whole php code running or part of your php code using XDebug. Or use the old method with var_dump(), die(), echo, and print_r(). And debugging is not an easy job for most developer.
As i state in the previous post, we can still do debuggin with Zend Studio and Zend server, but it’s too expensive. The alternative is we can use Eclipse PDT. Eclipse PDT is a free IDE for PHP supporting xdebug out of the box.
How to debug you PHP code with Eclipse PDT:
1. Install Eclipse PDT
Eclipse PDT (PHP Development Tools) is written with Java, so i assume this will work on most operating system. Download Eclipse PDT according to your platform. Just unpack it and install it then follow the wizard, and you are done.
2. Configure XDebug
The PHP server with xdebug can run on a another system than the one running the xdebug client. That is why xdebug is called a remote debugger. For simplicity, we will set up the debugging server and client on the same computer. To do that add this following to your php.ini:
xdebug.remote_enable=On xdebug.remote_host="localhost" xdebug.remote_port=9000 xdebug.remote_handler="dbgp"
When you start Eclipse, you might see a warning that Java is trying to set up a server, bind to a port, access the network, or perform some obscure potentially dangerous action. Of course, it’s not dangerous, it’s just the xdebug client trying to listen at port 9000. If you have problems with debugging, check if there are any firewalls between the debug server and the debug client that might block port 9000.
3. Configure Eclipse PDT
First of all, we will configure Eclipse to launch projects in an external browser instead of its internal browser. When an external browser is configured, all debugging sessions will be launched in an external browser as well. To configure Eclipse PDT to use XDebug, in preferences expand the PHP subtree and the Debug subtree of PHP. Then, change PHP debugger to Xdebug and click Apply.
Now create a new debug configuration, in menu bar click Run > Open Debug Dialog > PHP Web Page. Then choose Xdebug as Server Debugger. In the File / Project text field, you must enter the path to the script you want to debug. This path must be relative to your workspace. Apply to save the changes.
4. Debugging a PHP script
To start debugging a PHP script, choose Run from the menu bar and select Debug. You can add a breakpoint anywhere in your code, and run your code step by step, just like using Visual Studio.Net.