Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Joseph S.
January 30, 2006, 11:06 am
rate this thread
debugging PHP applications interactively is possible, easy and free. I
am talking about PHPEclipse and using it for debugging over several
scripts or debugging through a session. Since I have wasted a lot of
time writing echo statements all over the code in order to confirm
program flow and watch variables, I feel I must share this with others
who will be facing similar problems.
PHPEclispe users can go to Step 5 directly.
(People who already have software which does all this or feel that
their software is the best suited, please ignore this post :-))
First you need to get PHP and Apache.
(My OS is Windows XP Pro.) The best choice IMHO is XAMPP, as it allows
you to do many things - my version is 1.4.15 and it has, apart from
Apache2.0.54 and PHP5.0.4, phpMyAdmin2.6.3, FileZilla FTP, Webalizer
and many more things. It also allows you to switch between PHP4 and
So, get XAMPP at http://apachefriends.org .
Second, you need to get PHPEclipse.
For that you need Eclipse first. Eclipse can be got from
http://eclipse.org . There are two releases of Eclipse - Eclipse 3.0 and
Eclipse 3.1 (although I use 3.0, it seems 3.1 is the better one - also,
there are other free products like JSEclipse which need 3.1 - sooner or
need Eclipse 3.1) Installing Eclipse 3.0 on windows was as simple as
unpacking the zip into a folder, say c:\eclipse and double-clicking on
eclipse.exe to start. I've not tried 3.1 but I believe the installation
should not be any more difficult.
Before downloading anything first read up the PHPEClipse related wiki
page at http://www.plog4u.org/index.php/Using_PHPEclipse
Also read up http://www.plog4u.org/index.php/PHPEclipse_FAQ
NOTE: PHPEclipse runs from within Eclipse as module ("plugin").
After you install Eclipse and PHPEclipse, comes the important part. I'm
just writing the steps which confuse most - since you have all the
necessary help needed available at
http://www.plog4u.org/index.php/Using_PHPEclipse in detail. Assuming
you have PHP files ready which you want to debug, you need to create a
new project in PHPEclipse. Go to Window->Show View->Navigator. This
shows the navigator pane. Now right-click to get a pop up menu which
gives among other options New->Project. Click that and choose PHP->PHP
Project in the next form in the wizard. Click Next to get the New
Project (PHP Project) form. There you have a place to give your project
a name and a place to specify the folder (you will miss this one if you
do not look closely as you need to uncheck a checkbox labelled "Use
default"). Now specify the folder location in the folder selection box.
This step troubled me for quite some time.
Follow http://www.plog4u.org/index.php/Using_PHPEclipse for
intermediate steps like getting and installing the DBG debugger
For debugging, you start by first setting up the environment by setting
paths and options in PHPEclipse. Click on Run menu in Eclipse, then
"Debug ..." (Debug dot dot dot). A window appears with a
navigator/explorer pane on the left and many tabs on the right. Give a
name (anything) to the debug configuration. Enter the paths in the File
tab and the Envirnment tab. Environment has two sub-tabs - Remote Debug
and Interpreter. In Remote Debug, check the Remote Debug checkbox and
(MOST IMPORTANT) uncheck the "Open DBGSESSION url in internal browser"
checkbox. This is needed, we will soon see why. Also, set the Remote
SourcePath to be a part of the full path to your php script. The rule
about the RemoteSourcePath is that
(RemoteSourcePath in the Environment tab) + (File path in the File tab)
= actual full path to your php file.
On the Interpreter sub-tab, if you have set things up correctly (with
XAMPP) you should see a php and a php5 option. Choose either. If
nothing is shown, you can add an interprter by setting a name for it
and the path for it from the adjoining "New..." button. Try a few
things and you should be able to start a debug session in about an hour
if not in a few minutes.
Finally, run the Debug configuration (i.e., start the debugger) It will
now switch the window layout and show an active red button in the debug
toolbar with a few debug details in a prominent debug pane. It is now
_waiting_ for a request from any browser application. Set a breakpoint
in your PHP code (double clicking on the thin margin at the left of the
source code window, next to the text area). Also, set a breakpoint in
the next file that will get called when the user performs a form
submission in you first page.
Now, open your favorite webbrowser (independent of PHPEclipse) and
enter the url :
of these should work. A moment or two after you enter this (PHPEclipse
must be in the waiting-to-debug mode) you will see control switching to
PHPEClipse and the line in your first page where you set a breakpoint
will get highlighted by the debugger. Step through to the end. and
switch back to your web browser (where page two is now being
displayed). Click on a form submit and see how control switches back to
PHPEclipse on the line in the second page where you had put a
Thus you continually switch between your web browser and PHPEclipse.
The process can go on to as many number of files as you wish - just set
breakpoints in all the files before starting debugging. Trust me, it is
a good alternative to writing echo statements all over the place to
trap errors in a 15-20 page PHP application.
NOTE: On Windows, when control is switched to PHPEclipse, the Windows
taskbar button for PHPEclipse blinks, indicating that that program
demands interaction. I dont know how it is on Linux.
The important thing is to not start the debugger in the internal
browser. For that you must uncheck the checkbox saying "Open DBGSESSION
url in internal browser" which can be found in
Run->"Debug..."->Environment tab->Remote Debug tab. Somehow, (on my
machine at least) the internal browser does not support debuging over
Hope this helps someone, because I've wasted a lot of time adding
echo's and exits to see the state of variables in various places in my
scripts and it is quite tedious. That kind of debugging has its merits,
though - you become really well versed with your code!