How To Debug PHP In Eclipse On Windows
March 28, 2019

In this tutorial, we will discuss the terms specific to debugging and the steps required to debug PHP program in Windows using Eclipse IDE for PHP. It assumes that both Java and PHP are already installed on the system.

Notes: You may follow How To Install PHP 7 On Windows to install the most recent version of PHP 7. The other relevant tutorials include How To Install OpenJDK 12 On Windows, How To Install Java 11 On Windows, How To Install WampServer on Windows, and How To Install Eclipse for PHP on Windows.

Debugging Terms

This section explains all the major terms specific to debugging. These are listed below.

Current Instruction Pointer - The instruction pointer pointing to the current statement where the debugger is paused and waiting for next instructions.

Breakpoint - The program execution pauses at the breakpoint until further instructions are not provided by the debugger. The breakpoints are added intentionally to check the program for possible error.

Step Into - Move to next statement in case there is no function call or enter the function to debug it in case there is a function call on the current statement. We can step into the function called on the current statement to further check it. The execution will pause at the first statement of the function.

Step Out or Step Return - Execute the remaining statements of the function completely and move out of the function and set the instruction pointer on the statement next to the function call.

Step Over - Execute the current statement without going into the function if it's there. It skips the function and executes it without entering into it.

Resume - Resume the execution as the program executes normally till the next breakpoint encounters. The program execution will pause at next breakpoint if there is any.

Pause - Pause the current execution. The instruction pointer will point to the statement where the execution pause.

Stop - Terminate the current execution of the program and clear the variables stack and breakpoints from memory.

Install PHP

Install the most recent version of PHP. You can either install it using WampServer or separately.

Install Eclipse for PHP

Next, install the most recent version of Eclipse for PHP as explained here.

Hello World

Write the HelloWorld PHP program in Eclipse as explained here.

Configure Eclipse for PHP and Xdebug

In cases where PHP is installed separately, you must create your own php.ini using the php.ini-development or php.ini-production in the PHP installation directory. You can also configure the extensions directory in php.ini file as shown below.

extension_dir = "E:\tools\php\php-7.3.1\ext"

Use the Xdebug link to check whether it's already installed and activated for PHP. Write a simple PHP program having below-mentioned code to get the details about PHP.

// Prints PHP info on Console

Now execute and copy the content from the console and paste it on the Xdebug site as shown in Fig 1.

PHP Xdebug

Fig 1

Click on the Analyse my phpinfo() output Button. It shows that xDebug is not installed on my system and also provided instructions to install the same as shown in Fig 2. 

PHP Xdeug Install Steps

Fig 2

Move the downloaded DLL file to the ext directory of PHP installation. Note the php.ini location specified by Xdebug. It shows the location as my temp directory since Eclipse creates the php.ini in temp directories using the php.ini file configured for the installed PHP. You must always configure the correct php.ini file (as shown in Fig 4) instead of the one available at temp directory as mentioned in the instructions from Xdebug.

Make sure that you have added the zend_extension dll path at the end of the php.ini file. I have added the below-mentioned line at the end of my php.ini file as instructed by Xdebug.

zend_extension = E:\tools\php\php-7.3.1\ext\php_xdebug-2.7.0-7.3-vc15-x86_64.dll

Install Xdebug following the instructions and open the Preferences Dialog following Windows -> Preferences -> PHP -> Installed PHPs. It shows the installed PHPs as shown in Fig 3.

Installed PHP

Fig 3

The Preferences Dialog shows the currently installed PHPs. If it does not show any PHP version, you can always add your own by clicking on the Add button and navigating to the PHP install directory. Now select the PHP and click on Edit Button to configure the PHP. It shows the configuration wizard as shown in Fig 4. I have configured the PHP name, executable path, and the location of the php.ini file.

PHP Configuration

Fig 4

Now click on the Debugger Tab and select the Xdebug as the debugger for the selected PHP version as shown in Fig 5. In case the Debugger Tab complains about the Xdebug installation by showing a warning, follow the above steps carefully and restart your Eclipse.

Config Debugger

Fig 5

Press OK Button. It will show the configured debugger as shown in Fig 6.

Debugger Configured

Fig 6

Now press Apply and Close Button to close the Preferences Dialog. Configuring the debugger correctly is the most crucial step in order to debug PHP programs.

Basic Program to Debug

We will be updating the hello world program written in the previous section by adding a for loop to iterate and print the number spell on the console. The complete program is shown below.

$numbers = [ 'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine' ];

for( $i = 0; $i < 10; $i++ ) {

$number = $numbers[ $i ];

echo $number . "\n";

Debug the Program

In this section, we will debug the program by adding a breakpoint on line number 6 as shown in Fig 7.

PHP Program

Fig 7

Double click the left bar against line number 6. It will add a breakpoint as shown in Fig 8.

Add Brakpoint

Fig 8

The blue bubble on the left margin represents the breakpoint added by us. Now right click on the PHP program, hover on Debug As Option and click on PHP CLI Application Option as shown in Fig 9.

Start Debugger

Fig 9

It will show a dialog to switch to debug perspective as shown in Fig 10.

Switch Perspective

Fig 10

Click on OK Button to switch to debug perspective. The Debug Pointer will stop on line 1 as shown in Fig 11.

Fig 11 also shows the key buttons available for debugging.

Debug Options

Fig 11

Click on Step Over Button to execute the first statement. The pointer will move to line 4. We can also see that the variable $numbers is added on the Variables Stack as shown in Fig 12.

Step Over

Fig 12

Again click on Step Over Button to execute the for statement. It will move the pointer to line 6 and also add the variable $i to the Variable Stack as shown in Fig 13. Similarly, if we again click the Step Over Button, it will add the variable $number to the variables stack showing it's value set to Zero.

Step Over

Fig 13

Now click on the Resume Button and the normal execution starts until the code finds a breakpoint. The pointer will stop on line 6 since we have added a breakpoint on it. We can also see all the active breakpoints on Breakpoints Panel as shown in Fig 14.


Fig 14

If we double click on the breakpoint on line 6, it will be removed as shown in Fig 15.

Remove Breakpoint

Fig 15

Now again click on Resume Button and the program will be fully executed since the pointer won't find any other breakpoint. The complete output will be shown on the console as shown in Fig 16.

Final Output

Fig 16

This is the most basic way to debug a PHP program using Eclipse on Windows.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS