Bookmark and Share

Wednesday, February 17, 2010

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

Introduction


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.

Steps

* 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 http://xdebug.org/files/xdebug-2.0.5.tgz
> 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/xdebug.so /usr/local/php/modules/

Alter php.ini

Add the following lines to the php.ini :

# under the extensions ...
zend_extension="/usr/local/php/modules/xdebug.so"

# somewhere at the bottom ...
[xdebug]
xdebug.remote_enable=1

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: http://www.eclipse.org/downloads/download.php
* 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"
   [OK]
* 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" [
    [OK]
* right click (in index.php source window)
* select debug as PHP Web Page

Conclusions

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.

Links

* http://xdebug.org
* http://eclipse.org


phpinfo screenshot:

1 comment:

  1. I suggest to use free PHP IDE - Codelobster PHP Edition - http://wwww.codelobster.com
    It has own very convenient debugger.

    ReplyDelete