How To Debug PHP Web Apps In NetBeans On Windows

How To Debug PHP Web Apps In NetBeans On Windows

It provides all the steps required to debug PHP web applications in NetBeans IDE using Xdebug on Windows.

May 07, 2019

In the previous tutorials, we have discussed the steps required to install WampServer, PHP 7 or PHP 8, and NetBeans 11 or NetBeans 12. We have also discussed the steps required to debug PHP programs in CLI mode and remote debug PHP web applications as listed below.

In this tutorial, we will discuss all the steps required to locally debug PHP web applications using Xdebug, NetBeans and WampServer installed on localhost.

Create Project

The very first step is to create a project locally either directly in the www directory of the WampServer or Apache Web Server or any other directory with appropriate alias and configurations to access the project via the browser. Click on File -> New Project or press Ctrl + Shift + N to create a project in NetBeans. It will show the New Project wizard as shown in Fig 1.

Create Project Wizard

Fig 1

Also, select PHP under categories and PHP Application as the Project Option as shown in Fig 1. Click the Next Button to navigate to the Name and Location step. It will show the Name and Location wizard as shown in Fig 2.

Configure Project Name & Location

Fig 2

Provide the Project Name and Sources Folder based on your project needs. Also, select the appropriate PHP version matching with the version enabled in WampServer. Click on the Next Button to configure the Run Configuration.

Run Configurations

Fig 3

The Run As should be set to the local web server and the Project URL should be the URL to access the project on localhost as shown in Fig 3.

Click on the Next Button twice and leave the options to choose PHP Frameworks and Composer configurations blank. You can configure PHP Frameworks and Composer based on your project needs. Click the Finish Button to complete the project setup and create the project.

A default index.php file will be added to the project root with an HTML template as shown in Fig 4.

Project Defaults

Fig 4

Now update the index.php file as shown in Fig 5. Also, add the breakpoint as we did in previous tutorials.

Update Index File

Fig 5

Execute Project

Right click on the index.php file and click on the Run Option or press Shift + F6 to execute the program. It will open the browser and shows the output as shown in Fig 6.

Run Project

Fig 6

We can also configure the browser by right click on Project and then selecting the last option Project Properties. Now choose your preferred browser as shown in Fig 7 so that NetBeans will run and open your project in the selected browser.

Choose Browser

Fig 7

Debug Project

Right click on the index.php file and click on the Debug Option or press Ctrl + Shift + F5 to start the debug session. It will open the browser and shows the blank screen without executing the program since the debugger will pause at the first line of the program as shown in Fig 8.

Note that the URL of the blank screen in debug mode is set to http://localhost/ldebug/index.php?XDEBUG_SESSION_START=netbeans-xdebug having the debug session associated with it.

Debug Application

Fig 8

Now, click the Continue Button and the debugger will pause at line number 15 having the next breakpoint as shown in Fig 9.

Continue Debugging

Fig 9

Now again press the Continue Button and the first iteration of the for loop will execute. The debug pointer will again stop at line number 15 i.e. the next breakpoint as shown in Fig 10. Similarly, we can use the other options including Step Into, Step Over, Step Out, and Run to Cursor to find the issues in the code.

Continue First Iteration

Fig 10

Note that the value of $i is changed on the variables stack. In case we are sure that the issue is identified, we can remove the breakpoint and again press on the Continue Button to complete the execution. The browser will halt until the execution completes and shows the output at the end. The final output should be similar to the one shown in Fig 11.

Final Output

Fig 11

This is how we can remotely debug a local PHP web application using NetBeans and Xdebug.

Summary

This tutorial provided the reference links to install WampServer, PHP 7 or PHP 8, and NetBeans 11 or NetBeans 12. It provided all the steps required to locally debug a PHP project using the NetBeans IDE.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS