Testing error reporting
You will save a great deal of time if you leave error reporting enabled. Errors in PHP point you right to the error in your code. Create a simple PHP file, test.php, and define it as shown in Listing 1.
Listing 1. A simple PHP that generates an error
<?php print(”The next line generates an error.<br>”);
printaline(”PLEASE?”);
print(”This will not be displayed due to the above error.”);
?>
The first print() statement should display its contents to the Web browser. However, the second statement generates and displays an error to the Web page. This results in the last print() statement do nothing, as shown in Figure 1.
Figure 1. Generating an error
You have error reporting turned on! Next, we use print statements to help debug applications.
Introducing print statements
Because functional bugs in your application don’t generate errors, knowledge on how to accurately place and use print or die statements to debug your PHP application can be a great asset in your arsenal of debugging strategies. You can use print statements to narrow down the locations of problem statements in your code that may not be syntactically incorrect or bugs in the code, but they are bugs in the functionality of your code. These are the hardest bugs to find and debug because they throw no errors. You only know that what is being displayed to the browser isn’t what you intended, or that what you thought was being stored in your database isn’t being stored at all.
Suppose you are processing form data sent in via a GET request and want to display the information to the browser, but for whatever reason, the data is either not being submitted properly, or it isn’t being read from the GET request properly. To debug such a problem, it’s important to know what the value of the variable is, using a print() or a die() statement.
The die() statement halts program execution and displays text to the Web browser. The die() statement is particularly useful if you don’t want to have to comment out your code, and you only want everything up to the error and the error displayed and nothing after. Let’s test this concept of using print statements in PHP.
Debugging using print statements
In my years as a programmer where I developed applications on Linux® boxes with no handy GUI to tell me where my bugs were, I quickly caught on that the more print statements I laced my program with the more chances I had of narrowing down the bugs in my application to a single line. Create another PHP file, test2.php, and define it as shown in Listing 2.
Listing 2. Display all variables submitted via GET
<?php $j = “”;
print(”Lets retrieve all the variables submitted to this “);
print(”script via a GET request:<br>”);
foreach($_GET as $key => $i)
{
print(”$key=$j<br>”);
}
if($_GET[’Submit’] == “Send GET Request”)
$j = “done!<br>”;
?>
<form method=”GET”>
Name: <input name=”name”><br>
Email: <input name=”email” size=”25″><br>
<input name=”Submit” type=”submit” value=”Send GET Request”>
</form>
You may be able to spot the bug in Listing 2 very easily. Good for you! Note that this is a simple script, and is shown as an example in using print statements to debug. The script simply takes all variables in the GET request, if any, and displays them back to the browser. A form is also supplied for you to send variables to the server using a GET request for testing. See the output, as shown in Figure 2.
Figure 2. Output of test2.php
Now click the Send GET Request button. Notice that only the keys of the $_GET request got displayed to the browser, and the correct values did not. You can place a print statement within the loop to verify that data indeed exists at each element in the foreach loop. See Listing 3.
Listing 3. Using a print statement to verify code functionality
… foreach($_GET as $key => $i)
{
print(”Correct data? ” . $_GET[$key] . “<br>”);
print(”$key=$j<br>”);
}
…
The placed print statement is in bold font. Notice that you already know that the displayed $key values on the Web browser are correct, but for some reason, the values aren’t being displayed correctly. See the new output, as shown in Figure 3.
Figure 3. Output of modified test2.php
Now you know that your application is receiving the variables in the GET request correctly, so there must be a bug in your code. You look over and notice that the variable you are using for displaying the values, $j, is the wrong one. You specified $i in the foreach statement, which must have the correct values, but you accidentally typed in $j. You quickly fix the problem by replacing $j with $i and see the correct output by reloading the page, as shown in Figure 4.
Figure 4. Output of fixed test2.php
You can now remove or comment out the print statement you added because you have discovered the bug in your code. Notice that this is a small subset of the many errors you might experience while debugging your application. A good solution to a problem you may encounter when working with a database is to print out your SQL statements to make sure that what you are executing SQL you intended to execute.