Tito Miguel Costa
Refactoring ideas

How to debug a PHP project with Xdebug on NetBeans

No more error_log or var_dump in your code

Xdebug is a great tool to debug your PHP code, but make sure it is only used in your dev environment. It consumes a lot of memory and pages will load slower.

First thing is to install it. In my Ubuntu 12.10 box it was as simple as running:

sudo apt-get install php5-xdebug

Then I had to configure Xdebug editing the file /etc/php5/apache2/conf.d/20-xdebug.ini. The file will look like:

zend_extension=/usr/lib/php5/20100525/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.idekey="netbeans-xdebug"
xdebug.remote_log="/var/log/apache2/xdebug_remote.log"

Next step is to configure NetBeans. Check the properties of your project.

  • Make sure the Web root in the Sources categories points to your public forlder, web in a Symfony2 project
  • In the Run Configuration category, specify the name of your controller, in a Symfony2 project will be app_dev.php
  • Click on the Advanced button, and in the Debug URL, choose the Ask every time option. This way you can change the url to point to another location other than the index page.

Insert breakpoints in your code with Ctrl+F8 or just click over the line number, a red square will show up.

To start the debug, press Ctrl+F5 or click on the Debug Project button on the toolbar.

Edit the URL of the page you want to debug and press Start. A new tab on your browser will open up and will remain blank. Get back to NetBeans play with Debug button on the toolbar.

Everytime you reach a breakpoint, execution will stop and you can check on the debug window the values of your variables.

Check out the official NetBeans webpage for more info.

7 December, 2012
Tutorial
Xdebug, NetBeans, Symfony2, Ubuntu

Comments