My previous post touched upon the basics of getting Eclipse PDT (PHP Development Tools) up and running for PHP development. In this post, we’ll be looking at getting PHP debugging capabilities into our PHP dev environment. Again, I’ll be focusing on Ubuntu Linux – 8.04 LTS to be specific: however, with minor changes, this can be used on any platform.
Of course, there are simpler methods of debugging PHP scripts: a var_dump is quite handy in terms of simple debugging – however, having the right tools at hand for development means that we can spend more time in actual development than writing debug statements every once in a while – anyways, that’s my take.
XDebug is a remote debugger. Here we’ll take a look at getting XDebug working with Eclipse PDT providing a full debugging client. The first step would of course be to get Eclipse PDT up and running (refer previous post if needed). Once done, the next step would be to get Xdebug for our PHP environment set up – in my case, PHP5. On Ubuntu, this can be achieved by:
apt-get install php5-xdebug
This should install Xdebug for PHP5. Next, we’d configure Xdebug by editing the PHP configuration file. Most systems require the configuration items be added to the main php.ini, or the appropriate conf file. In Ubuntu 8.04 LTS, a separate configuration file,xdebug.ini, is created for Xdebug. Open this file using your favorite text editor (vim in my case):
sudo vim /etc/php5/conf.d/xdebug.ini
Add the following lines after the zend_extension= or zend_extension_ts= conf value.
And restart your web-server. In my case, Apache:
To try out the debugger, open a PHP project in Eclipse (if you are importing an existing project via CVS, remember to add it as a PHP project using the New Project Wizard). In Window->Preferences->General->Web Browser, select external web browser, and set it to point to your default web browser. Next go into the debug section, by Window->Preferences->PHP->Debug, and set the PHP Debugger option to XDebug (by default this is set to the Zend Debugger). Note the chekbox “Break at first line” in this page as well – you can change this later on, as required. After you’ve applied your changes, go into the Debug menu, by Run->Open Debug Dialog… Here, make sure the Server Debugger is set to XDebug, and that the PHP server points to http://localhost or an appropriate server location. The file section should point to the file to be debugged, relative to your workspace (Note: if your project is missing from the list, check whether you’re project is configured as a PHP project – I’ve noticed that non-PHP projects fail to appear on this list). Thus if your file is index.php, the relative path maybe something/www/index.php etc.
Next, check the URL section to see whether it points to the correct location – if not, make sure you uncheck the “Auto-Generate” check box, and enter in the correct location. Apply the changes and close the dialog box.
A view of the Debug dialog is here:
You should now be ready to debug your application. Click on Run->Debug or F11 to enable debugging – Eclipse should switch to the Debug View, and the page should start loading in your external browser. If “Break at First Line” checkbox was checked previously, then the program would have “stopped” at the first line – you can step into or step over subsequent lines using the controls on your top left corner. Alternatively, you can place breakpoints at various locations and stop code execution there as well. The variables tab should give you the variables and their values at various times. I good guide from which I borrowed from is here. Get creative – start debugging.
A view of the debugger in operation is here: