සිතිවිලි (Sithiwili)

ලංකාවේ සිට ජීවිතය හා තාක්ෂනය ගැන තබන සටහන (A Sri Lankan blog on life and technology)

Debugging PHP with PDT and XDebug

with 8 comments

This post is for PHP developers currently not using a proper PHP debugger.
Installation and configuration of PDT (PHP Development Tools for Eclipse) with the excellent XDebug debugger is discussed in a step-by-step fashion. Please note that my intention is to allow a beginner to get up and running quickly. So my apologies for not discussing all aspect of the procedure in detail. I must also warn you that there could be other (possibly better) ways of getting the same thing done.
සිංහල ලිපියක් ද හැකි ඉක්මනින් පලකරන්නම්.

Check PHP version and file locations

  1. Create a PHP file (say info.php) with the following code: <? phpinfo(); ?>
  2. Open it using a web browser.

phpinfo1.gif
According to this:

  • PHP 5.2.3 is installed. It is not a debug build and thread safety is enabled.
  • The location of the php.ini file is given as C:\ms4w\Apache\cgi-bin\php.ini
  • The PHP extensions directory is given as: C:\ms4w\Apache\php\ext

Note these settings for your installation of PHP.

Configure PHP to use XDebug

  1. Go to http://www.xdebug.org and download the correct windows module for your PHP version.
    In my case, php_xdebug-2.0.2-5.2.5.dll was used.
  2. Copy the XDebug dll file to the PHP extensions directory.
  3. Add the following at the end of the php.ini file. Just to be on the safe-side, make a backup of this php.ini file.
    [xdebug]
    xdebug.remote_enable=1
    xdebug.remote_host="localhost"
    xdebug.remote_port=9000
    xdebug.remote_handler="dbgp"
    zend_extension_ts="C:\ms4w\Apache\php\ext\php_xdebug-2.0.2-5.2.5.dll"
  4. In php.ini, find the implicit_flush directive and the output_buffering directive and set it as follows:
    implicit_flush = On
    output_buffering = 0

    Warning: this will seriously degrade performance so should be done only on the development server.

Notes

  • xdebug.remote_host is where your debugger client will run.
    You will need to change the path and file name of the xdebug dll file to match your environment.

Restart Apache server.
Open/refresh the info.php file from the web browser and verify that XDebug is installed correctly.
There should be an entry right-next the Zend logo …
phpinfo3.gif
… and a detailed XDebug configuration section further down the page.
phpinfo4.gif

Install PDT

  1. Download the all-in-one PDT bundle from http://www.eclipse.org/pdt/
    It comes as a zip file named pdt-all-in-one-1.0-R20070917-win32.zip
  2. Extract the zip file to C:\
  3. Execute C:\eclipse\eclipse.exe
  4. Select a workspace- this is where your projects will reside.
    Select the parent directory of where your web directories will be placed.
    pdt-workspace.gif

Create a PHP project

  1. File — > New — > Project … — > PHP — > PHP Project
  2. Select prj1 for project name and click Finish.
  3. Right clicking on the project folder of the PHP Explorer, and select New — > PHP file.
  4. Enter some PHP code. e.g.
    <?php
    for($i=1; $i<6; $i++){
    echo $i . "<br />";
    }
    ?>

Configure PDT as a XDebug client

Define a PHP executable: Window — > Preferences — > PHP — > PHP Executables — > New
pdt-2-php-executable.gif

Define the settings for a PHP Web Server: Window — > Preferences — > PHP — > PHP Servers
pdt-3-php-servers.gif
Notice that we define the root of the server to be our workspace directory.

Define debug settings: Window — > Preferences — > PHP — > Debug
pdt-1-debugger-3.gif

Go to Window — > Preferences — > General — > Web Browser
Select to use an external web browser.

To debug a PHP web application

Select: Run — > Open Debug Dialog …
Right click on “PHP Web Page” in the list on left-hand side and select “New …”
Select settings as below:

pdt-debugdialog.gif

You will have to type in prj1 part in URL.

Select “Apply” and then click “Debug”.
That’s it.

To debug a PHP Script

Just right-click file and select Debug As … — > PHP Script
That’s it.

Reference

For more details on using XDebug with PDT refer to the XDebug Guide.

Written by Nalaka(නාලක)

December 7, 2007 at 3:18 pm

Posted in PHP

Tagged with , ,

8 Responses

Subscribe to comments with RSS.

  1. Hi
    I found this very helpful and got it working. I was wondering do you also have smiliar tutorual of subeclipse?
    Regards
    Nik

    nikhil

    January 30, 2008 at 10:37 am

  2. Hello,

    Please, apologize me for my (very) poor english
    You use php by cgi.
    What are the setup of pdt when isapi are used ?

    Thanks by advance.

    Bernard MAYER

    March 11, 2008 at 8:56 pm

  3. Sorry. I haven’t used PHP with IIS.
    :(

    Nalaka(නාලක)

    March 11, 2008 at 9:25 pm

  4. [...] mentioning it because it’s hard to make it work with Eclipse. Thanks to the great blog post Debugging PHP with PDT and XDebug it is now easy to have debugging environment for your PHP projects. « Peter Hacker [...]

  5. Thanks for that article, got me running on IIS with no problem. Now I just have to learn PHP and I’m done!

    Thanks,
    Dan

    http://www.DVDs4theSAT.com
    Great SAT prep in a 12 DVD set

    Daniel Rosenstark

    May 3, 2008 at 3:50 am

  6. Here’s a detailed step-by-step article for setting up a PHP debugger using PDT+XDebug and XAMPP:

    http://robsnotebook.com/php_debugger_pdt_xdebug

    Rob

    June 12, 2008 at 9:07 am

  7. Nice quick article, was up and running in no time. I was setting this up on a friends computer and the quote used by your font in the code below caused my php.ini file not to work. If anyone else has this problem, be sure to replace [ ” ] character with a normal [ " ] characters

    [xdebug]
    xdebug.remote_enable=1
    xdebug.remote_host=”localhost”
    xdebug.remote_port=9000
    xdebug.remote_handler=”dbgp”
    zend_extension_ts=”C:\ms4w\Apache\php\ext\php_xdebug-2.0.2-5.2.5.dll”

    DAniel

    September 8, 2008 at 12:24 am

  8. Thank you. Worked great.
    You should make one adjustment, under Eclipse 3.4 the debug options are in: Run -> Debug Configurations, but this should be logical.

    Thanks again.

    mihai

    October 24, 2008 at 3:13 pm


Leave a Reply