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.

  1. Download the Zend Server: http://www.zend.com/en/products/server/downloads (ZendServer-6.3.0-php-5.3.28-Windows_x86.exe - Use this link to download the Zend Server 32 with PHP 5.3 for Windows).
  2. Start the installation
  3. 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)"
  4. 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! =) 
  5. 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/). 
  6. 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.
  7. 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. 
  8. Keep the option "Start working with Zend Server" checked and finish the install pressing the Finish button.

 

2. Zend Server configuration

After install Zend Server, open the url http://localhost:10081/ 

  1. License Agreement: Read the term, check the box, and click Next.
  2. Launch Type: Select Development and click Next.
  3. User passwords: Define the administrator password, keeping the developer password blank, and click Next.
  4. Library packages: Wait for all them reach 100% and click Next.
  5. Summary: This screen shows the settings summary. Click Launch.

If everything is fine, you should see the Zend welcome screen:

 

3. Testing Zend Server

Open the URL http://localhost:81 or http://localhost:80 (depending on the port you chose at the step 1.5). You must see the Zend Server Test Page:

Go to the folder C:\Program Files (x86)\Zend\Apache2\htdocs. This is the place where the webserver documents are located. Erase everything and create a blank index.html.

 

4. Eclipse install

Go to the Eclipse project download page (http://www.eclipse.org/downloads/) and download the Eclipse Standard last version.

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

To install the PDT (PHP Development Tools) package, select Help -> Install New Software.

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):

Select Window -> Preferences. Then, select PHP -> PHP Servers. Select the item "Default PHP Web Server", and edit it. Change the "Base URL to http://localhost:81.

3. Select the Web Browser

I the Preferences window (Window -> Preferences), go to General -> Web Browser, check the option Use external web browser and select your favourite one. 

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!