Followers

Tuesday, January 20, 2009

HOWTO: Using xdebug and PHPEclipse for PHP debugging on Windows

TAGS: php xdebug phpeclipse

After struggling for a while to get the ability of debugging PHP scripts, reading here and there on xdebug wiki, and other php developers websites, I finally managed to assemble the needed information into this HOWTO which I hope is going to help someone to setup the proper environment for using PHPEclipse and xdebug to debug PHP scripts.

To debug using xdebug extension and PHPEclipse editor on windows (vista or other versions should equally the same) there are two main steps:

A- Installation of xdebug.
B- Preparing PHPEclipse.

Follow the steps exactly as explained and you will be safe.

= = = = = = = = = = = = = = =

A- Installation of xdebug:

If you are familiar with php extensions, just download the xdebug extension and modify php.ini to load it as a zend extension, and skip reading to section B.

In details, follow these five steps:

1- Download the xdebug http://www.xdebug.org/download.php.
make sure to select the proper version matching the server threaded mode.
I am using WampManager 2.0 ( which is bundled with PHP5, MySQL, Apache 2.x)

2- Extract the dll file and put in the ext folder of your PHP.
(in my case, c:\wamp\bin\php\php5.2.5\ext\php_xdebug.dll , match your php version and path)

3- Open php.ini and put these lines at the end of the file

zend_extension_ts = "C:\wamp\bin\php\php5.2.5\ext\php_xdebug.dll"
xdebug.remote_enable=1


(remember, the path must match your php installation path)

NOTE:
extension=php_xdebug.dll
to load xdebug as an extension instead of a zend_extension was not helpful! I tried it and wasted time trying to make it work, avoid it unless you know what you are doing.


4- Restart Apache.
With WampManager, it is as easy as a click on the WampManager Icon and choosing Restart All Services.


5- After Apache restarts, open the phpinfo page to check out xdebug is loaded properly.
if you dont have a phpinfo page, just create a new phpinfo.php file as follows:



If the dll was inserted in the correct path, and the php.ini was updated correctly, then you will see the following:

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
with Xdebug v2.0.4, Copyright (c) 2002-2008, by Derick Rethans



If you can not see that line, go over the steps.
1- Check your Apache version.
2- Check your PHP version.
3- Check the dll name (that it has no version number, only php_xdebug.dll).
4- Download the correct xdebug version if needed and repeat the above steps.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

B- Using xdebug in PHPEclipse

1- Create a script you want to debug.

2- Create brake points by a Double Click on the line margin, or right click -> Toggle Breakpoint.


You will notice small blue dot at each breakpoint location.


3- Open Debug Configurations ( in Run menu).



4- Double Click the PHP Debug Remote Script option.

5- Specify the IDE identification string , for example: testID.
(it is the ID we will use to start debugging later on, important to choose something easy to retype).



The project name is filled automatically, and you can type the folder name which the script will be saved it, if you have not created a project already.

6- Click on PATHMAP tab.

7- Click on NEW and type the path to your script.


8- Click Debug button.
(the window will close and you will be taken back to the PHP editor).




9- Click on Debug Perspective Button (found on the top right corner, if not, window -> show perspective -> debug).



the perspective will be empty but of the original code.


10- Point your browser to the test.php script

in my case, http://localhost/debug/test.php



So far, no debugging session has started, check the PHPEclipse:



but once you click on debug, which passes
?XDEBUG_SESSION_START=testID&test=1
the debug session will start. You will notice the perspective windows in PHPEclipse starting to populate with debug info and variable values.



Stepping in / out is possible by clicking the yellow arrows in the Debug perspective.


That's it. Enjoy :)

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

No comments:

About Me

My photo
Chicago, IL, United States
Creativity enthusiast ... Worked as web based application developer for more than 10 years using cgi, asp, php, asp.net server scripting languages in addition to JavaScript and recently glowing frameworks such as prototype, scriptaculous, jquery, mootools.. working on enhancing managerial skills and looking forward for a good position ;)