Bookmark and Share

Wednesday, February 17, 2010

Debug PHP code in an IDE in less than one hour.


Although RawDev offers debugging tools, it can be useful to use an IDE with a debugger. I will show you how to setup PHP debugging using open source software for both command line and web page debugging.

 This example uses eclipse as the IDE and XDebug as the PHP module, note that this is not the only software available. You need root access and already have apache and php installed. The instructions are for Mac OS X / Unix but should be very similar for Windows users.


* Setup XDebug
  * Install XDebug
  * Alter php.ini
* Install Eclipse
* Hello World example
  * Create Hello World example
  * Debug as PHP Script (command line)
  * Debug as PHP Web Page

Setup XDebug

Install XDebug (easiest)

> pecl install xdebug

Install XDebug (unix / alternate)

> cd /usr/local/src wget
> tar xvfz xdebug-2.0.5
> cd xdebug-2.0.5
> phpize
> ./configure --enable-xdebug
> make
> # mkdir /usr/local/php/modules # if it doesn't exist
> cp modules/ /usr/local/php/modules/

Alter php.ini

Add the following lines to the php.ini :

# under the extensions ...

# somewhere at the bottom ...

Test XDebug

* restart apache
* create a script (info.php) in your web_root folder.
* check the script and make sure XDebug is enabled see the screenshot at the bottom of document.

Install Eclipse

* download Eclipse for PHP Developers from:
* install it

Hello World example

Create example

* run eclipse
* >File>New>Project>PHP>PHP Project
* Use default path: "~/Documents/workspace/Hello"
* Project Name "Hello" [Next] [Finish] [Yes]
* >File>New>PHP File
* Name : index.php [Finish]

Debug as PHP Script (command line)

* >settings>PHP>PHP Executables>Add
* Name: "php" ;
   Path: "/usr/local/php/bin/php" ;
   php.ini: "/usr/local/php.ini" ;
   PHP Debugger: "XDebug"
* right click (in index.php source window)
* select debug as PHP Script

Debug as PHP Web Page

* Make sure the project path is visible in the webroot
* one way: create a symbolic link to project directory and
                   project directory is visible for all (e.g. chmod 755)
* test this in a browser
* >settings>PHP>PHP Servers>Add (&make default)
* Name: "php" ;
    Path: "/usr/local/php/bin/php" ;
    php.ini: "/usr/local/php.ini" ;
    PHP Debugger: "XDebug" [
* right click (in index.php source window)
* select debug as PHP Web Page


Obviously these are instructions just to get you started. The other debugging module is "Zend Debugger" and other PHP IDE's are available (e.g. Komodo). RawDev will use more of XDebug extended functionality such as profiling.



phpinfo screenshot:

1 comment:

  1. I suggest to use free PHP IDE - Codelobster PHP Edition -
    It has own very convenient debugger.