As you already know, when someone writes code in a high-level language there are two types of errors that he or she might make—syntax errors and logic errors. NetBeans IDE provides all the necessary tools to help you debug your scripts and find the errors.
Debugging syntax errors
Fortunately, NetBeans IDE detects syntax errors while you are typing and underlines them with a wavy red line as shown in Figure 1.
Figure 1 In the NetBeans IDE, syntax errors are underlined with a wavy red line
All you have to do is correct the corresponding error and the red line will disappear at once. However, if you are not certain about what is wrong with your code, you can just place your mouse cursor on the red exclamation mark. NetBeans IDE will try to help you by showing a popup window with a brief explanation of the error, as shown in Figure 2.
Figure 2 The NetBeans IDE shows an explanation of a syntax error
Debugging logic errors by executing scripts step by step
Compared to syntax errors, logic errors are more difficult to find. Since the NetBeans IDE cannot spot and underline logic errors, you are all alone! Let’s look at following PHP script, for example. It prompts the user to enter two numbers and calculates and displays their sum. However, it contains a logic error!
<?php $s = 0; echo "Enter 1st value: "; $a = trim(fgets(STDIN)); echo "Enter 2nd value: "; $b = trim(fgets(STDIN)); $S = $a + $b; echo "The sum is: ", $s; ?>
If you type this script into the NetBeans IDE, you will notice that there is not even one exclamation mark indicating any error. If you execute the script (SHIFT+F6), however, and enter two values, 5 and 2, you can see for yourself that even though the sum of 5 and 2 is 7, NetBeans IDE insists that it is zero, as shown in Figure 3.
Figure 3 Viewing the result of a logic error in the Output window
What the heck is going on? Of course for an expert programmer, correcting this error would be a piece of cake. But for you, a novice one, even though you are trying hard you find nothing wrong. So, where is the error?
Sometimes human eyes get so tired that they cannot see the obvious. So, let’s try to use some magic! Let’s try to execute the script step by step using the debugger. This gives you the opportunity to observe the flow of execution and take a closer look at the current values of variables in each step.
Start the debugger by selecting “Debug →
Debug File” from the main menu or by hitting the CTRL+SHIFT+F5 key combination. By doing this, you enable the debugger and more icons are displayed on the toolbar.
Notice: In order to enable the debugger (XDebug), you need to modify the PHP.ini file. More information here.
The program counter (the green arrow icon in the gray margin) stops at the first line of the script as shown in Figure 4.
Figure 4 Using the debugger in the NetBeans IDE
Notice: The program counter shows you, at any time, which statement is the next to be executed.
Click on the “Step into” icon on the toolbar. This action tells the PHP interpreter to execute the statement $s = 0
, and the program counter moves to the second PHP statement, which is the next to be executed.
From the main menu, select “Window →
Debugging →
Variables”. In the window that appears, you can watch all the variables declared in main memory (RAM) and the value they contain during each step of execution. Until now, the only variable declared and assigned a value is the variable $s
as shown in Figure 5.
Figure 5 The value of variable $s is displayed in the Variables window of the debugger
Click on the “Step into” toolbar icon again. The second statement is executed. You can go back to the “Output” window and see the output result (see Figure 6).
Figure 6 Viewing a prompt in the Output window
Click on the “Step into” toolbar icon again. The third PHP statement is executed and the program waits for you to enter a value. Place the cursor inside the “Output” window, type the value 5, and hit the “Enter ↵” key.
The program counter moves to the fourth PHP statement. If you go back to the “Variables” window you can see that, at the moment, two variables ($a
and $s
) are declared in the main memory (RAM), as shown in Figure 7.
Figure 7 The values of variables $a and $s are displayed in the Variables window
Click on the “Step into” toolbar icon twice. This action executes the fourth and fifth PHP statements. Place the cursor inside the “Output” window, type the value 2, and hit the “Enter ↵” key.
The program counter moves to the sixth PHP statement. If you go back to the “Variables” window you can see that, at this moment, three variables ($a, $b
and $s
) are declared in the main memory (RAM) as shown in Figure 8.
Figure 8 The values of variables $a, $b
and $s
are displayed in the Variables window
Click on the “Step into” toolbar icon once again. The statement $S = $a + $b
is executed. What you expected here was the sum of 5 and 2, which is 7, to be assigned to variable $s
. Instead of this, a new variable $S
is declared in the main memory (RAM) and the value 7 is assigned to it, as shown in Figure 9.
Figure 9 All declared variables are displayed in the Variables window of the debugger
Now it becomes more obvious to you! You mistakenly declared two variables in the main memory (RAM), the variable $s
and the variable $S
with a capital S. So, when the flow of execution goes to the last statement, echo "The sum is: ", $s
the value 0 instead of the value 7 is displayed.
You have just found the error! Click on the “Finish Debugger Session” toolbar icon to cancel execution, correct the error by changing variable $S
to $s
, and you are ready! You just performed your first debugging! Re-execute the script and you will now see that it calculates and displays the sum correctly.
Debugging logic errors by adding breakpoints
Debugging step by step has a big disadvantage. You have to click on the “Step into” toolbar icon again and again until you reach the position where the error might be. Can you imagine yourself doing this in a large script?
For large scripts there is another approach. If you suspect that the error is somewhere at the end of the script there is no need to debug all of it right from the beginning. You can add a marker (called a “breakpoint”) where you think that the error might be, execute the script and when flow of execution reaches that breakpoint, the flow of execution will pause automatically. You can then take a closer look at the current values of the variables at the position where the script was paused.
Notice: When the script pauses, you have two options for resuming the execution: you can add a second breakpoint somewhere below in the script, click on the “Continue” toolbar icon, and allow the script to continue execution until that new breakpoint; or, you can just use the “Step into” toolbar icon and execute the script step by step thereafter.
The next PHP script prompts the user to enter two values and calculates their average value.
However, the script contains a logic error. When the user enters the values 10 and 12, the value 16, instead of 11, is displayed.
<?php echo "Enter 1st value: "; $a = trim(fgets(STDIN)); echo "Enter 2nd value: "; $b = trim(fgets(STDIN)); $average = $a + $b / 2; echo "The average value is: ", $average; ?>
You suspect that the problem is somewhere at the end of the script. However, you do not want to debug the entire script, but just the portion in which the error might be. So, let’s try to add a breakpoint at the $average = $a + $b / 2
statement (see Figure 10). There are two ways to do this: you can click in the left gray margin on the corresponding line number, or you can place the cursor at the line of interest and hit the CTRL+F8 key combination.
Figure 10 Adding a breakpoint to a script
Notice: You know that a breakpoint has been set when the pink rectangle appears in the gray margin and the corresponding line has pink background highlighting it.
Hit CTRL+SHIFT+F5 to start the debugger. Click on the “Continue” toolbar icon and enter the values 10 and 12 when requested. You will notice that just after you enter the second number and hit the “Enter ↵” key, the flow of execution pauses at the breakpoint. (You will see that the pink breakpoint highlighting is replaced by the green highlighting of the program counter.)
Remember! You can debug a file by selecting “Debug
→
Debug File” from the main menu or by hitting the CTRL+SHIFT+F5 key combination.
Now you can take a closer look at the current values of the variables. Variables $a
and $b
contain the values 10 and 12 respectively, as they should, so there is nothing wrong with the data input, as shown in Figure 11.
Figure 11 Viewing the current values of two variables in the Variables window
Click on the “Step into” toolbar icon once. The statement $average = $a + $b / 2
executes and the main memory (RAM) now contains the following values (see Figure 12).
Figure 12 Viewing the current values of three variables in the Variables window
There it is! You just found the statement that erroneously assigns a value of 16, instead of 11, to the variable $average
! And now comes the difficult part; you should consider why this happens!
After two days of thinking, it becomes obvious! You had just forgotten to enclose $a + $b
inside parentheses; thus, only the variable $b
was divided by 2. Click on the “Finish Debugger Session” toolbar icon, remove all breakpoints, correct the error by enclosing $a + $b
inside parentheses and you are ready! Re-execute the script and see now that it calculates and displays the average value correctly.
Notice: You can remove a breakpoint the same way you added it: click in the left gray margin on the corresponding line number, or place the cursor at the line that contains a breakpoint and hit the CTRL+F8 key combination.