How to build a PHP debug environment, under Windows

The PHP language is a versatile option to develop since websites, to full web based systems, because of the high level of integration with the HTML sctructure, object orientation support and the fact of not requiring build and compilation. However, depending on the desired complexity, a good debugger and a development environment may be requisites.

In this article I explain how I use to build, under windows, a full PHP development environment, with debug support, using the Eclipse's last version available, because PDT All-In-One package is usually outdated one version or two of the last stable build.

1. Install Zend Server

The Zend Server if a Apache-based webserver, that has PHP support and debug features, easy to install and configure.

If want to take the opportunnity to install a local MySQL server, at the Setup Type screen, choose "Custom" and check the option "MySQL Server (separated download)"

At the WebServer screen, keep checked the option "Install an Apache xxxx Web Server". If you want to go with the ISS, I wish you Good Luck! =)

At the Apache Port Number screen, I prefer changing the port to 81, for security reasons. But if you want to keep 80, no problem. The second field if the port used to access the server administration panel (http://localhost:10081/).

Then, click the Install button. If you chose to install the MySQL server, it will be downloaded at this moment. The process takes a few minutes.

Before finishing the install, the system will ask you about the Apache HTTP server (httpd.exe) permissions. Keep checked the Private Networks options and click Allow Access. - here, I apologize for the Portuguese screenshot, but I think it's enough to understand this step.

Keep the option "Start working with Zend Server" checked and finish the install pressing the Finish button.

4. Eclipse install

To install the Eclipse, you must just unpack the compressed file inside any folder. Then, run eclipse.exe.

When running for the first time, you will be asked for your workspace location, that is, the folder where are located your projects. Select the folder C:\Program Files (x86)\Zend\Apache2\htdocs (I suggest using it as default location for your PHP projects). Check the "Use this as default..." option. If you need to change the workspace location, go to File -> Switch workspace.

Close the "Welcome" tab and you will see the environment default screen, totally blank.

5. Install PHP Development Tools in Eclipse

Fill the Work with field with the following URL: http://download.eclipse.org/tools/pdt/updates/release and press [Enter].

Expand the group PHP Development Tools, select the option PHP Development Tools (PDT) and click Next.

If everything is fine, you will see a Install Details screen, just a summary of the items about to be installed. Click Next, accept the licence terms and click Finish. Eclipse will download all packages automatically. At the end, will be requested that you restart the Eclipse so the changes can make effect.

6. Eclipse configuration

1. PHP Perspective

Enable the PHP perspective, as shown in the picture:

2. Web Server address

You must configure your webserver address (if you chose the 81 port for the Zend Server, as mentioned in the step 1.5):

3. Select the Web Browser

4. Avoid the debugger stop at every file first line

By default, the debbuger always stop at every file first line. As I don't like very much this behavior, I disable it: In the Preferences window (Window -> Preferences), go to PHP -> Debug and uncheck the option Break at first line

7. Creating and debugging your first project

Look for the PHP Explorer tab, right-click, and select New -> Project.

Select the option PHP Project and click Next.

Set the project name as test and click Finish.

Right-click the project entry, select New -> PHP File, and create a index.php file, with the following code:

<?php
$a = 1;
$b = 2;
$c = $a + $b;
echo "Result = ". $c;

Save the file, and put a breakpoint at the line $b = 2; (double-click the indicated region).

To debug, with the index.php file open, expand the debbuger option at the toolbar (as indicated in the picture), and select Debug As -> PHP Web Application.

Another way to do it is right-clicking the source code, or the file itself in the PHP Explorer (it's up to you). Then, the webbrowser you chose at the step 6.3 will open. Go back to the Eclipse window.

It will be asked if you want to open the Debug Perpective. I use to accept and remember the decision, because it has many useful debug panel (breakpoints, variables inspect etc).

If nothing is wrong until here, you already have a PHP debug environment ready to go, using Eclipse latest version and the browser of your choice. Enjoy!