Monday, May 27, 2013

Eclipse and XDebug


PHP Debugging in Eclipse

I have googled my way through many different forums and faced many problems in configuring Eclipse for PHP debugging. So this post is to document the steps so that its easier for any newbie to look and follow through.
  • Install Eclipse June(4.2.x) from the Eclipse site. Download the classic version http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops4/R-4.2.2-201302041200/eclipse-SDK-4.2.2-win32-x86_64.zip * Install the PHP Development tools plugin. In the Eclipse IDE, go to Help->Install New Software-> Add http://download.eclipse.org/tools/pdt/updates/release
  • Now the PHP development environment is setup.
  • To setup a local server, we can use the Xampp server which has the Apache, PHP, MySQL in a single package. Download it fromhttp://www.apachefriends.org/download.php?xampp-win32-1.8.1-VC9.zip . Note that this is an extractable copy which you can directly use without installing. (You can copy it extract the Zip file and copy the Xamp directory to C:\)
  • We have to configure the php ini file for xdebug. Go to C:\xampp\php\php.ini and go to the end of the while where Zend extension is configured. We have to correct it with C:\ path like this zend_extension = “C:\xampp\php\ext\php_xdebug.dll” Since this is important configuration details I am pasting the XDebug configuration below. Except the zend_extension and xdebug.remote_enable are commented and unused for now. (We can use other parameters if we want to set up remote debug).
  •  [XDebug]
    zend_extension = "C:\xampp\php\ext\php_xdebug.dll" 
    ;xdebug.profiler_append = 0
    ;xdebug.profiler_enable = 1
    ;xdebug.profiler_enable_trigger = 0
    ;xdebug.profiler_output_dir = "\xampp\tmp" 
    ;xdebug.profiler_output_name = "cachegrind.out.%t-%s" 
    xdebug.remote_enable = 1
    ;xdebug.remote_handler = "dbgp" 
    ;xdebug.remote_host = "127.0.0.1" 
    ;xdebug.trace_output_dir = "\xampp\tmp"
  • Go to C:\xampp and double click on xampp-control.ext. Choose language/country and then it will show a control panel to start/stop server. Start Apache server from there.

Create a new PHP project

  • Then start the Eclipse IDE with workspace as C:\xampp\htdocs.
  • Create New PHP project. (From File->New->PHP->PHP Project and give a name say MySite and click Finish). Since this is the first time we are creating PHP file, it will prompt to open PDT perspective and choose Ok.
  • Create a new PHP file by right clicking on the project folder in PHP Explorer. New->PHP File and name it as test.php. You can add a sample code in test.php file to test
    <?php
    $a =20;
    echo $a;
    ?>
    .
  • Now we have to setup the debugger to use XDebug. Go to menu Window->Preferences->PHP->Debug and under Debug Settings Choose XDebug as the PHP Debugger instead of Zend. Click Apply and Ok.
  • Right click on the test.php file and choose Debug As-> PHP Web Application. It will show a dialog with the URL and Click Ok. Now it will break in the first line of the PHP file. You can also set breakpoints in the php file before starting the execution and it will break at breakpoints