සිතිවිලි (Sithiwili)

Debugging PHP with PDT and XDebug

Posted in PHP by Nalaka(නාලක) on December 7, 2007

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.

Tagged with: , ,

12 Responses

Subscribe to comments with RSS.

  1. nikhil said, on January 30, 2008 at 10:37 am

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

  2. Bernard MAYER said, on March 11, 2008 at 8:56 pm

    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.

  3. Nalaka(නාලක) said, on March 11, 2008 at 9:25 pm

    Sorry. I haven’t used PHP with IIS.😦

  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. Daniel Rosenstark said, on May 3, 2008 at 3:50 am

    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

  6. Rob said, on June 12, 2008 at 9:07 am

    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

  7. DAniel said, on September 8, 2008 at 12:24 am

    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”

  8. mihai said, on October 24, 2008 at 3:13 pm

    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.

  9. Edwin Martin said, on November 20, 2009 at 6:41 pm

    If you use PHP 5.3 and can’t get XDebug appear in your phpinfo():

    “From PHP 5.3 onwards, you always need to use zend_extension and not zend_extension_ts.”

    See http://www.xdebug.org/docs/install

  10. alex said, on January 25, 2010 at 9:59 pm

    Hi,

    I’ve installed Eclipse with XDebug Under Xampp following your tutorial but when it comes to define the PHP Executables I got stuck , I do not know Where / How to get them !?

    many thanks

  11. aarbrown said, on June 4, 2011 at 11:54 am

    Fantastic! I’d been using the ‘echo’ method of debugging PHP scripts for years. I’ve been doing more and more PHP scripting recently and found the need for an IDE with debugging. PDT + Xdebug + Wamp do an amazing job for me. Thanks for the tutorial!

  12. youtube views increaser said, on July 23, 2013 at 2:04 am

    It’s amazing in support of me to have a web page, which is useful in support of my know-how. thanks admin


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: