How To Debug PHP In NetBeans On Windows
How To Debug PHP In NetBeans On Windows
May 05, 2019

In this tutorial, we will discuss the terms specific to debugging and the steps required to debug PHP program in Windows using the NetBeans IDE for PHP. It assumes that PHP is already installed. You can follow How To Install PHP 7 On Windows to install the most recent version of PHP 7.

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 has been paused.

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. This tutorial explains debugging the PHP script in CLI mode using the separately installed PHP.

Install NetBeans for PHP

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

Hello World

Write the HelloWorld PHP program in NetBeans as explained here.

Configure 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 must 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.

Xdebug with phpinfo

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.

Xdebug Wizard

Fig 2

Download and move the downloaded DLL file to the ext directory of PHP installation. Note the php.ini location specified by 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
zend_extension = E:\tools\php\php-7.3.1\ext\php_xdebug-2.7.1-7.3-vc15-x86_64.dll

In order to debug PHP using NetBeans, you must also enable remote debugging by adding below mentioned configurations to the php.ini file.

; Zend Extension
zend_extension = E:\tools\php\php-7.3.1\ext\php_xdebug-2.7.1-7.3-vc15-x86_64.dll

; Enable remote debug

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 highlighted in Fig 3.

PHP Program for Debugging

Fig 3

Now click on the left bar against line number 6. It will add a breakpoint as shown in Fig 4.

Add Breakpoint

Fig 4

The red square on the left margin represents the breakpoint added by us. Now right click on the PHP program, and click on Debug Option as shown in Fig 5. You can also use the shortcut keys Ctrl + Shift + F5 to start the debugger.

Start Debugger

Fig 5

The debugger will show the Windows Security dialog for the first time as shown in Fig 6.

Windows Security

Fig 6

Click on the Allow access Button to allow the debugger to start on the port 9000 as configured by us.

The debug pointer will pause on the 1st line by default as shown in Fig 7. We can change this behavior by configuring NetBeans. Fig 7 also highlights the debugging tools.

Debugger Terms

Fig 7

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 8.

Step Over

Fig 8

Again click on Step Over Button to execute the for the statement. It will move the pointer to line 6 and also add the variable $i to the Variable Stack as shown in Fig 9. 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 2

Fig 9

Now click on the Continue 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 click Run to Cursor Button to pause the debugger at the next Breakpoint. We can also see all the active breakpoints on Breakpoints Panel as shown in Fig 10.


Fig 10

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

Remove Breakpoint

Fig 11

Now again click on Continue 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 12.

Continue Debugging

Fig 12

This is the easiest way to debug a PHP program using NetBeans on Windows.

Write a Comment

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