How to debug the PHP code with the use of NeatBeans IDE?

Context Menu Keypresses
image_pdfimage_print

In this article I’d like to explain in detail how to debug the PHP code with the use of NeatBeans IDE.  NeatBeans IDE itself can be downloaded here. Debugging is done on the local computer, so it is necessary to have local server installed on the computer which would ensure the work with PHP as well as with the database.

So, we need to start NeatBeans IDE and after that it is necessary to create the new project for debugging.  Enter File->New Project.

A new project in NetBeans IDE

As we can see, to create the project we are offered two options.

Choose PHP project

The first: you can create the new project.  The second: to indicate already existing one.  We specify the existing project:

Name and location for the PHP Project with existing sources

Here it is necessary to specify the path where the project is, the title of the project as well as the coding with which we will work.  Then we need to select the configuration executed on default:

Setting of the type of the configurations executed on default


As we can see in the picture, it is possible to set three types of the configurations executed on default:

  • Local server. In order to use this type of the configuration, you need to install and to start the server Apache. How to install the local server, please, read here, you can download it here.
  • Remote server (FTP).  In order to use this type of configuration, you must get registered on the remote server and have an account FTP on this server.
  • Script. This configuration does not require the installation of the local server on your computer. What you need is to install PHP.

In this article we will work with the first option.  So we need to press Finish.  Our project is opened. When you have created the new project, it is possible to:

1.      Open the project in the browser:Run project

2.      Open the project in the browser’s new window for debugging: Debug project

If you have opened the project for debugging, in the browser you’ll see the message:

Launching.
Please wait…

It means that single-step debugging has started. Use NetBeans for debugging!

Using NetBeans we will notice that the first open file is index.php and in the very first line there is green arrow, which indicates that the debugging starts from here. It should be noted that the debugger passes only PHP code written between “<?”  and “?>”. In order to stop debugging at the needed place, you can use so called Break points.  In order to identify the break point, it is necessary to press Ctrl+F8 or with the click of the left button of the mouse on the line on the left:

Set Breakpoint

For effective work with the debugger the following combination of the buttons can be helpful:

Combination of the buttons in the PHP project

  • To stop debugging – Shift+F5
  • To continue debugging – F5
  • To step over – F8.  When you meet for instance the function call, the button F8 allows skipping this function without entering inside it.
  • Step in – F7.  When you meet for instance the function call, the button F7 allows entering the body of the function and doing debugging inside the function.
  • Step out – Ctrl+F7. When we are for instance inside the function, the button F7 allows coming out of the function and doing debugging one level higher.
  • Go until cursor – F4. The button F4 allows going until the line lower, where cursor is positioned.


Why doing single-step debugging?  Mainly to see where in our program the fault is.  Usually checking the values of variables helps to find an error. How can we know which value has the variable at the moment?  Usually it is sufficient to point the mouse pointer at the variable, if the variable is of ordinary type, for instance, string one:

The pop-up window with the value of the variable

in the pop-up window we can see the value of the variable $test.  And what if the variable is of complicated type, for instance, a massive?  Then this variable should be added to the panel of variables’ values.  Select, for example, the variable $myrow, and then open the context menu with the left button of the mouse and select there:

Creation of the New watch

On the panel you’ll see the variable $myrow, then it will be possible to see its value.

The panel with the value of the variable in them

In this way you can debug the code and find errors. I’ll be glad to answer your questions.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *