Visual Studio Code and Syntax Errors
When someone writes code in a high-level language they might make some mistakes. Look at the following PHP script.
<?php $num1 = 5; $num2 = 10; $c = $num1 + $num2 + ; echo c; ?>
Fortunately, Visual Studio Code detects syntax errors while you are typing and underlines them with a wavy red line, as shown in Figure 1.
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 erroneous line. Visual Studio Code will try to help you by showing a popup window with a brief explanation of the error, as shown in Figure 2.
Visual Studio Code and Runtime Errors
Now, look at the following PHP script.
<?php $num1 = (float)readline("Enter number A: "); $num2 = (float)readline("Enter number B: "); $c = $num1 / $num2; echo $c, "\n"; ?>
The script may look perfect, but have you thought about the possibility of $num2
being zero? Unfortunately, the way this script is written permits the user to enter a value of zero for variable $num2
. If you try to execute this script, and enter a value of zero for $num2
, PHP’s interpreter complains and displays the error “Division by zero” (Figure 3). Also, the interpreter tells you that this runtime error happened when line 4 was executed!
Notice: Using a decision structure, the computer could determine whether it should perform this division. We won’ t deal with this problem right now! It was used just to demonstrate how Visual Studio Code displays a run-time error when it occurs.
Visual Studio Code and Logic Errors – Debugging Step-By-Step
Compared to syntax errors, logic errors are more difficult to find. Since Visual Studio Code cannot spot and mark logic errors, you are all alone! Let’s look at the following PHP script, for example. It prompts the user to enter two numbers and calculates and displays the square of their sum. However, it contains a logic error!
<?php $a = (int)readline("Enter 1st value: "); $b = (int)readline("Enter 2nd value: "); $Sl = $a + $b; $S1 = $Sl ** 2; echo "The square of sum is: ", $Sl, "\n"; ?>
If you type this script into Visual Studio Code, you will notice that not a single line is underlined with a wavy red line, indicating any error. However, if you run the script and enter two values, 2 and 3, you will see for yourself that even though the square of the sum of 2 and 3 is 25, Visual Studio Code insists that it is 5, as shown in Figure 4.
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.
Place the cursor at the line where the statement $a = (int)readline("Enter 1st value: ")
exists and hit the F9 key. A red bullet appears in the left margin as shown in Figure 5.
Start the debugger by selecting “Run → Start Debugging” from the main menu or by hitting the F5 key. By doing this, you enable the debugger and more buttons are displayed on the toolbar.
The program counter (the yellow arrow icon in the left margin) stops at the first line of the script, as shown in Figure 6.
Notice: The program counter shows you, at any time, which statement is the next to be executed.
In this step, the statement
is not yet executed. Click on the “Step Over” $a = (int)readline("Enter 1st value: ")
button on the toolbar or hit the F10 key once. Now the statement
is executed, and the script waits for you to enter a value. In the “Console” window, type the value 2, and hit the “Enter ⤶” key.$a = (int)readline("Enter 1st value: ")
The program counter moves to the next PHP statement. Click on the “Step Over” button again. In the “Console” window, type the value 3, and hit the “Enter ⤶” key.
In the Side Bar on the left, you can watch all the variables declared in main memory (RAM) and the value they contain during each step of execution, as shown in Figure 7.
Click on the “Step Over” button again. The third statement
$Sl = $a + $b
is executed.
Click on the “Step Over” button once again to execute the fourth statement
$S1 = $Sl ** 2
. What you expected here was the sum of 2 and 3, which is 5, to be squared and assigned back to variable Sl
. Instead of this, the value 25 is assigned to the variable S1
, as shown in Figure 8.
Notice: Variable
$Sl
is different from$S1
. The first one uses the characters S and L (in lowercase), while the second one uses the characters S and 1 (digit one).
Now it becomes more obvious to you! You mistakenly declared two variables in the main memory (RAM), the variable $Sl
with a lowercase L and the variable $S1
with a digit 1. So, when the flow of execution goes to the last statement, echo "The square of sum is: ", $Sl, "\n"
the value 5 instead of the value 25 is displayed.
Congratulations! You have just found the error! Click on the “Stop” button to cancel execution, correct the error by changing variable $S1
to $Sl
, 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 square of the sum correctly.
Debugging logic errors by adding breakpoints
Debugging step by step has a big disadvantage. You have to click on the “Step Over” button 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”) near 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” button, and allow the script to continue execution until that new breakpoint; or, you can just use the “Step Over”
button and execute the script step by step thereafter.
The next PHP script prompts the user to enter two values and then it calculates their sum, their difference, and 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 as the average.
<?php $a = (float)readline("Enter 1st value: "); $b = (float)readline("Enter 2nd value: "); $s = $a + $b; $d = $a - $b; $average = $a + $b / 2; echo "Sum:", $s, "\n"; echo "Difference:", $d, "\n"; echo "Average:", $averagea, "\n"; ?>
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 $d = $a - $b
statement (see Figure 9). There are two ways to do this: you can click in the left margin on the corresponding line, or you can place the cursor at the line of interest and hit the F9 key.
Notice: You know that a breakpoint has been set when the red circle appears in the left margin.
Start the debugger by selecting “Run → Start Debugging” from the main menu or by hitting the F5 key. Enter the values 10 and 12 when requested in the “Console” window. You will notice that just after you enter the second number and hit the “Enter ⤶” key, the flow of execution pauses at the breakpoint (the corresponding line has yellow background highlighting it).
Notice: You can run a project by selecting “Run → Start Debugging” from the main menu or by hitting the F5 key.
Now you can take a closer look at the current values of the variables. Variables $a
, $b
, and $s
contain the values 10, 12, and 22 respectively, as they should, so there is nothing wrong so far, as shown in Figure 10.
Click on the “Step Over” button once. The statement
$d = $a − $b
executes and variables $a
, $b
, $d
and $s
contain the values 10, 12, −2, and 22 respectively, as they should, so there is still nothing wrong so far, as shown in Figure 11.
Click on the “Step Over” button once again. The statement
$average = $a + $b / 2
executes and variables $a
, $average
, $b
, $d
, and $s
contain the values 10.0, 16.0, 12, −2, and 22 respectively, as shown in Figure 12.
There it is! You just found the statement that erroneously assigns a value of 16.0, instead of 11.0, to the variable $average
! And now comes the difficult part; you should consider why this happens!
After two full 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 “Stop” button, 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 on the corresponding breakpoint, or place the cursor at the line that contains a breakpoint and hit the F9 key, or right click on the corresponding breakpoint, and select “Remove Breakpoint”.