PHP Debugging with Zend Studio


This article was published by, 2008-02-03, as a feature tutorial.

Web developers who wish to use their time as efficiently as possible, should consider using an integrated development environment (IDE), if they are not already doing so. Just as the name suggests, a typical IDE provides the developer with an integrated suite of programming products, including an editor with multi-file editing, multiple undo and redo, local and global search and replace, stream and paragraph text selection, code-folding, multi-line tabbing, automatic creation and coloring of matching braces, intelligent indenting and outdenting of code, and more.

For the majority of experienced PHP developers, Zend Studio is considered the top-of-the-line PHP IDE. The product is put out by Zend, a company heavily involved with PHP, dating back to 1997. In fact, they created and contributed PHP's execution engine, the Zend Engine.

In this tutorial, we will explore how to use the latest update of Zend Studio for debugging PHP scripts. But first, we will get an overview of the product, its various editions, the system requirements for running it, and other installation considerations.

Overview and Product Editions

During its early history, Zend Studio was built upon a platform developed in-house by the Zend staff. Their newest version, however, is now built upon Eclipse, the increasingly popular open source platform. In addition, Zend Studio for Eclipse uses the PHP Development Tools (PDT) framework. Zend continues to publish and support the non-Eclipse line, which is now at version 5.5.1.

Even though the Eclipse line is the future direction of the product, in this tutorial we will focus on version 5.5.1, because 5.x is the version set probably being used by most readers of this tutorial.

Like so many other software products nowadays, Zend Studio comes in two editions: Standard and Professional. The Standard edition has a full source code editor (supporting both PHP 4 and 5), as well as an interactive debugger for working with local PHP scripts. The Professional edition provides a number of capabilities beyond editing and debugging: remote debugging, code analysis and optimization, source management, version control, and many others.

System Requirements

The system requirements of Zend Studio are quite similar to those of other IDEs: In terms of operating systems, it supports Windows 2000, XP, 2003, and Vista (only 32 bit), Linux x86 and x86-64, and Mac OS X 10.4 with an Intel x86 or a PowerPC G4 or G5 processor. The company's system requirements page does not appear to specify the minimum RAM, but I can attest that it runs fine — alongside several other nontrivial applications, and Windows XP — on a PC equipped with 1 GB of system memory.

As for disk space, the installation file for Zend Studio is less than 74 MB, while the installation size on disk is approximately 174 MB.

Installation and Start Up

The first step to using Zend Studio is to download the latest version from the product page. Choose the appropriate release and operating system package for your particular development machine. Even if you do not have a product license, you can download it and try it out during a 30-day evaluation period.

There are a number of steps needed to install Zend Studio. During this process, you can choose to have the Zend Internet Explorer and Firefox toolbars added to your Web browser(s). You can have the full PHP manual installed, which would be especially handy if and when you are working without access to the Internet, such as during air travel. You can change the target installation directory from the default one, and specify which file extensions to be associated with Zend Studio. You also have the option of installing Zend Core (which consists of libraries, extensions, remote debugging capabilities, etc.) and Zend Platform (a PHP application server).

User Interface and Preferences

When you run Zend Studio for the first time, you will see the default user interface pictured below.

Taking center stage is the Editor window. The other windows are as follows, in counterclockwise order, starting in the upper left: The File Manager is what you use for working with files, projects, and SQL Server. The Inspectors window is for the file, project, and PHP inspectors. The debug Messages window is at the bottom left-hand corner. The Debug window — for monitoring variables, watches, etc. — is to the right of that. The main window on the right side is for Debug Output; its two tabs display the textual and Web output of the script currently being debugged.

Like any robust IDE, Zend Studio lets you modify not only the placement and size of its various windows, but a wide range of other settings as well: desktop, editor, code completion, colors and fonts, debugger, Zend Platform, keymaps, file types, templates, source control, dialog boxes, and command key strokes. All of these settings are accessible via the Tools > Preferences menu item.

Debugging Basics

A primary raison d'être of any programmer's IDE is the ability to execute source code, one line at a time, and monitor the program's output, its execution path, and the changing values of its internal variables. Zend Studio's debugger is no exception.

The best way to see these capabilities in action, and to learn how to do them yourself, is to follow a straightforward example. In this case, we will use the Debug Demo that is included with Zend Studio. To begin, choose File > Open File, browse to your Zend Studio installation directory, and open the directory bin\examples\debugdemo (in this tutorial, any path names will be in Windows format). Open the file DebugDemo.php. You should see the following in your user interface:

Due to image size limitations in this tutorial, the details of each of the main windows may be difficult to discern. Also, the Debug Output window needs to be widened. So first we will minimize the File Manager and Inspectors windows. They can always be restored by clicking on their named icons in the leftmost column within the overall window.

Now you can increase the width of the Debug Output window. In the Editor window, you will see the Debug Demo code. It is a simple PHP script that first defines an array of worker information, and then uses two different functions to display that information in an HTML table.

The various Zend Studio debugger tools can be accessed from either the debugger toolbar, or the Debug menu. On the debugger toolbar, the functions of the buttons, from left to right, are:

Back, Forward, Step Over, Step Out, Step Into, Go, Go to Cursor, Run, Stop Debugger, and Pause Debugger.

All of these functions are available from the Debug menu, as well as some additional ones. Also, the menu bar shows the hotkey combinations, for those who prefer utilizing key commands instead of clicking toolbar icons or selecting menu items.

Stepping Through the Code

Using the Step Over command several times, we can step down through the code to line number 59, which calls the display_workers() function. If we then use the Step Into command, the debugger goes to line 43, which is the first line of code in that function. If we step forward to line 48, the $worker_data array has been populated with the first record. In the Debug window, if the Variables tab has been selected (as it is by default), then you can see the current defined variables, including that array. Clicking the "+" symbol expands the array, showing the individual elements and their current values.

As you continue to Step Over the lines of code, cycling within the "for" loop, you will see the elements of $worker_data automatically updated.

In the Debug Output window, in the Text panel, you will see all of the HTML resulting from the code executed up to that point.

The HTML tab shows the output as displayed by a Web server.

If at any point you Run the debugger to the end of the script, the Text tab will show the completed Web code.

The HTML tab shows the final Web page.

Breakpoints and Conditions

Stepping through each individual line of code is fine if there are few of them, as in our example, or you want to see the effects of most if not all the lines of code. But what if you are only interested in a bit of code in a huge program? That's where breakpoints become indispensable. Imagine that the very first line of display_workers() — where the database handle $db is made global — is where you first want to begin debugging. Click on the line number (in this case, 43) and the entire line is colored pink, indicating that a breakpoint has been set on that line. Then when you run the Go command, the debugger executes all the way up to that line, and stops, regardless of how deep in the code the line is located.

Perhaps you would like the debugger to execute the "for" loop until the loop counter, $i, is equal to 3. In that case, you would first set a breakpoint on that line. Then, set a condition for that breakpoint: In the Debug window's Breakpoints tab, right-click on the breakpoint, select "Edit Condition", and enter "$i = 3" as the watch expression. Then use the Go command to execute up to that point where $i is equal to 3, which is confirmed in the Variables tab in the Debug window.


Once you have worked your way through the Debug Demo project, as we have done above, you will likely want to try out the debugger on your own project. To get started, choose New from the File menu. At that point you can choose to create a Zend Framework project, or a regular PHP project, or a non-PHP generic project.

The above discussion should be more than enough to get you started on using the debugging capabilities of Zend Studio, and dramatically increase the ease and speed with which you are able to debug your PHP code.

Copyright © 2008 Michael J. Ross. All rights reserved.