Debugging PHP using Eclipse and PDT
Use XDebug or Zend Debugger to boost your productivity when
fixing bugs in PHP applications
Skill Level: Intermediate
Nathan A. Good (mail@nathanagood.com)
Senior Information Engineer
Consultant
17 Jun 2008
The PHP Development Tools (PDT) plug-in, when installed with Eclipse Europa,
gives you that ability to quickly write and debug PHP scripts and pages. PDT
supports two debugging tools: XDebug and the Zend Debugger. Learn how to
configure PDT for debugging PHP scripts and discover which perspectives you use
when taking closer looks at your scripts.
Section 1. Before you start
About this tutorial
This tutorial demonstrates how to configure the PHP Development Tools (PDT)
plug-in for Eclipse to debug your PHP scripts. It also introduces the perspectives
you'll use (namely, PHP Debug) when taking closer looks at your PHP scripts.
Objectives
After completing this tutorial, you'll be able to set up either XDebug — an open
source project that allows you to debug executable scripts and scripts running on a
Web server — or the Zend Debugger in Eclipse using the PDT project to develop
Debugging PHP using Eclipse and PDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 1 of 35
developerWorks®
ibm.com/developerWorks
PHP applications. You'll understand the various parts of the PDT project's PHP
Debug perspective and learn how to set up, view, and work with breakpoints. You
also learn how to inspect the values of variables as you are stepping through the
code, as well as how to debug PHP Web applications on your local server so you
can run through your PHP Web application with the debugger.
Prerequisites
To get the most out of this tutorial, you should have done a bit of PHP development.
But what matters more is that you've done software development in general. You'll
understand the debugging concepts better if you're familiar with debugging any other
language. I wrote this to be helpful to those who are fairly new to Eclipse, rather than
to those who have been using Eclipse for a while.
System requirements
To complete this tutorial, you need:
Computer running Microsoft® Windows®, Mac OS X, or Linux®The examples of the php.ini file shown in this tutorial are for Mac OS X and
Linux. Because both debuggers require a configuration that tells PHP where
the debugger extensions are located, the only noticeable difference — if you're
looking for Windows examples — is the path to the debugger extension. Library
names end in
.so. for Mac OS X and Linux and
.dll for Windows; also, paths
use a forward slash (/) instead of a backslash (\) as a directory-separator
character.
PHP V5.xLinux users can install PHP using the software package system included in
their distribution. OS X, and Windows users can find PHP V5.x at PHP.net.
Note: The examples in this tutorial were written using PHP V5.2.5.
Eclipse V3.3Again, Linux users have it easy: Eclipse is usually available through the
package system included in their distribution. Everyone else can find the
Eclipse integrated development environment (IDE) at the Eclipse downloads.
Apache or Microsoft Internet Information Services (IIS) for serving Web
applicationsYou need a Web server installed to run the examples that demonstrate how to
debug PHP Web pages on the server. However, if you're interested only in
debugging PHP scripts that aren't Web pages, you won't need a Web server.
For this tutorial, we have Apache V2 set up as the Web server. If you're using
Debugging PHP using Eclipse and PDT
Page 2 of 35
© Copyright IBM Corporation 1994, 2008. All rights reserved.
ibm.com/developerWorks
developerWorks®
Windows and would rather use IIS, that works, too. This tutorial assumes you
have the proper access to write to directories inside the document root of the
Web server.
Eclipse PHP Development Tools frameworkIf you don't already have PHP Development Tools (PDT) installed, you may
want to read ahead to the "Overview of the PDT project" section so you can
decide whether you want to download and install PDT already bundled with the
Zend Debugger. The Eclipse Foundation PDT/Installation wiki is brief, but
helpful.
Zend Studio Web DebuggerDownload a 30-day trial version of the Zend Studio Web Debugger.
Section 2. Getting started
Debugging PHP the old way
Before being able to use a debugger inside an IDE, my main methods for debugging
PHP scripts were to:
1.
Set up a bunch of echo statements that printed variable values. I'd have
to take these out or comment them out before deploying the script.
2.
Use "I am here" echo statements to print the position in the scripts. I'd
also have to remove these lines of code or comment them out before
deploying the script.
3.
Tweak the php.ini file to tell PHP to print verbose messaging, including
warnings. Doing this can be a security concern, as it may display things
you don't want to display. If you're doing development locally and
deploying the scripts to a different server, this is less of an issue.
4.
Use a logger class, such as the Log PEAR module. This is a great option
because the logger allows you to set priorities so you can quiet debugging
messages later. However, it requires an amount of time — albeit relatively
small — to configure and to come up with a process for having different
logging levels in different environments. Typically, you might want to see
"debug" messages in development environments, but only "warning"
messages or above in production environments. Regardless of your
Debugging PHP using Eclipse and PDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 3 of 35
developerWorks®
ibm.com/developerWorks
debugging technique, I recommend finding and using a logging
framework for your PHP applications. A drawback of debuggers is that
sometimes developers are tempted to neglect the time investment of
adding proper logging.
All these techniques, while they worked fine for me for a few years, consume much
more time than stepping through an application in a debugger. So, you can save a
great deal of time using a debugger from inside the IDE. The setup of both
debuggers — XDebug and the Zend Debugger — is covered here.
Overview of the PDT project
The PDT plug-in, when installed with Eclipse Europa, gives you that ability to quickly
write and debug PHP scripts and pages. The PDT project supports two debugging
tools: XDebug and the Zend Debugger. This tutorial demonstrates how to configure
PDT for debugging PHP scripts using either debugger.
PDT V1.0 was released in September 2007. The tools work with the Web Tools
Platform (WTP) to provide the ability to build PHP projects and PHP files with
Eclipse. The PDT project provides features you'll be used to if you're already using
Eclipse for Java™ development — like the PHP perspective and the PHP
Debugging perspective. The PHP editor has syntax highlighting, code formatting,
syntax verification, and code templates. Using PDT, you can execute PHP scripts
and run them on a Web server, and PDT also supports debugging PHP files locally
and on a server (although the debugging requires a bit of setup). This tutorial
focuses on configuring PDT to use one of the supported debuggers for PDT:
XDebug or the Zend Debugger by Zend Software (see Resources).
You don't need both debuggers installed; you can pick and use one of them. This
tutorial covers both, allowing you to make a decision about which one you would like
to use and install. When you have one installed, you can debug any PHP script. With
the PHP Debug perspective, you can view variable values and breakpoints as well
as step through the code. The next section dives into the different parts of the
perspective and how you use them.
Getting ready for this tutorial
Web server-accessible folders
The Apache Web server supports a feature called
UserDir, or user
directories, where the Apache Web server looks for a folder
matching a preconfigured folder name and, when found, maps it to
a URL with a tilde (~) appended to the user name. On Mac OS X,
this folder is called
Sites by default; on other operating systems, it
has names like
public_html or
public_www or even
www. Check
your Web-server configuration because limiting your project's
Debugging PHP using Eclipse and PDT
Page 4 of 35
© Copyright IBM Corporation 1994, 2008. All rights reserved.
ibm.com/developerWorks
developerWorks®
contents to your home directory can be a really good idea. If you
have IIS, it's convenient enough to create a virtual folder mapped to
a directory in which you can place your project contents. As a last
option, create a folder directly under your Web server's document
root and place the project resources in that.
After you've installed PDT, you must add a couple of projects you can use to follow
along. The first is a simple script that prints a greeting a certain number of times.
The second project is a Web application with two pages. These examples work fine
with either debugger, and you don't need to set up the debuggers before adding
these projects.
Add the simple projectThe simple PHP project with a simple example script demonstrates the features of
the debuggers and PDT Debug perspective. Perform the following steps to create a
simple PHP project to use as an example if you don't already have an example
available. (Alternatively, download the code from the Download section.)
1.
Choose
New > Project, then select
PHP Project from the list, as shown
in Figure 1, then click
Next.
Figure 1. Adding a PHP projectDebugging PHP using Eclipse and PDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 5 of 35
developerWorks®
ibm.com/developerWorks
2.
Type simpleDebugExample for the project name, as shown below.
Select the
Use default checkbox, then click
Finish.
Figure 2. Entering the project informationDebugging PHP using Eclipse and PDT
Page 6 of 35
© Copyright IBM Corporation 1994, 2008. All rights reserved.
ibm.com/developerWorks
developerWorks®
3.
When the project appears in your workspace, add the file in Listing 1.
Listing 1. helloworld.php<?php
$name = "world";
for ($i = 0; $i %gt; 100; $i++) {
print("Hello, $name ($i)!\n");
}
?>
Add the Web application sampleThe Web application example has a couple of Web pages so you can see how to
Debugging PHP using Eclipse and PDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 7 of 35
developerWorks®
ibm.com/developerWorks
use the debuggers to debug PHP Web pages on the server from page to page.
You'll be able to see how you can catch variables posted from one page to another.
To set up the Web application example:
1.
Choose
New > Project to add a new project, then select
PHP Project
from the list, as shown in Figure 3, then click
Next.
Figure 3. Adding a PHP Web project2.
Modify where the project contents reside.
Unlike the simple example, you may need to modify where the project
contents are located. To make such a modification, clear the
Use default
checkbox, then click
Browse to find a suitable location for your PHP Web
Debugging PHP using Eclipse and PDT
Page 8 of 35
© Copyright IBM Corporation 1994, 2008. All rights reserved.
ibm.com/developerWorks
developerWorks®
pages. Because these Web pages need to be executed on the server,
you can either build your entire workspace in a location where your Web
server can reach it or tweak this setting to put certain package contents in
an accessible location. I don't like having my whole workspace accessible
by the Web server, so I use this setting to put only my Web application
contents in a folder. My setting, for example, is
/Users/nagood/Sites/webDebugExample. On my computer, that directory
is accessible at the URL http://localhost/~nagood/webDebugExample.
3.
After adding the new project, add three new files. The first —
enterGreeting.php — is shown in Listing 2.
Listing 2. enterGreeting.php<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<?php
$name = "world";
print("<b>Hello, $name</b>");
?>
<form action="results.php" method="post">
<input type="hidden" name="name" value="<?php print($name); ?>" />
<input type="text" name="greeting" value="" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
4.
Add the second of the three files: results.php. The contents of this file are
shown in Listing 3.
Listing 3. results.php<html>
<head><title>Results</title>
</head>
<body>
<?php
include_once 'GreetMaster2000.php';
print ("Hello, " . $_POST['name'] . "!");
print ("<br />");
$gm = new GreetMaster2000();
$gm->setGreeting($_POST['greeting']);
Debugging PHP using Eclipse and PDT
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 9 of 35
developerWorks®
ibm.com/developerWorks
print ("<b>Your greeting is:
<i>" . $gm->getFormalGreeting() .
"</i></b>");
?>
</body>
</html>
5.
Add the third of the three files — GreetMaster2000.php — which is shown
in Listing 4. This file contains a class to demonstrate what classes look
like when they're debugged.
Listing 4. GreetMaster2000.php<?php
class GreetMaster2000
{
private $greeting;
private $name;
public function __construct()
{
$this->name = "The GreetMaster 2000 (model Z)";
}
public function setGreeting($message)
{
$this->greeting = $message;
}
public function getGreeting()
{
return $this->greeting;
}
public function getFormalGreeting()
{
return "I, the " . $this->name . ", say to you:
\"" .
$this->getGreeting() . "\"";
}
}
?>
Section 3. The PHP Debug perspective
Before setting up the debuggers and starting to debug the projects you've just
created, familiarize yourself with the PHP Debug perspective so you feel comfortable
debugging your PHP application.
The first time you choose
Debug As > PHP Script or
Debug As > PHP Web Page
to debug a PHP file, Eclipse asks if you want to switch to the PHP Debug
Debugging PHP using Eclipse and PDT
Page 10 of 35
© Copyright IBM Corporation 1994, 2008. All rights reserved.
Document Outline
Add New Comment