PHP Debugging

PHP Debugging Basics

Things don’t always go according to plan. So when you’re writing new code, chances are that it won’t work the first time you run it. Realistically programming is as much about debugging as it is about coding. PHP Debugging is a huge subject and well worth spending time learning, but there are some key tools and techniques that we can go over briefly.

PHP syntax errors

First, let’s create some buggy code!

1
2
echo ‘Here is our buggy script’
echo ‘It will not run.

If you run the code above, you’ll see something like this:

Note the value in the brackets: that’s the line with the problem. Look at that line of code. It contains a syntax error: the line must be terminated with a semicolon. Change the script to this:

1
2
echo ‘Here is our improved script’;
echo ‘It will run ok.;

Errors like these are usually not hard to find: PHP will refuse to run code that contains syntax errors and will try to identify the line that has the issue. These types of bugs are call Syntax Errors.

Logic Errors
Logic Errors are another kind of bug, and these can be harder to find. Logic errors occur when the PHP code is syntactically valid (ie it will run), but it has flaws that make it not generate the output we expect. Let’s take a look. Create a new PHP document:

1
2
3
4
echo 'Numbers from 1 to 10:';
for($n = 1; $n < 10; $n++ ) {
echo '<br>' . $n;
}

Run this and note that it doesn’t do what it says it should. Debugging this example is straight-forward: $n never makes it to 10 so something is preventing the loop from reaching that number. If we examine the for block we notice that the condition of our for loop is that $n < 10. If we change it to $n <= 10 and run the script again, it works as expected.

Here’s a script with a more subtle logic error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function checkFor($valueToCheck) {
   // var_dump($valueToCheck);
   $validFlavors = array(‘chocolate’, ‘strawberry’, ‘mint’, ‘cookies and cream’);
   // var_dump($validFlavors);
   $found = false;
   foreach($validFlavors as $flavour) {
      // var_dump($flavor);
      if ($flavor == $valueToCheck) {
         echo ‘found ’ . $valueToCheck;
         $found = true;
      }
   }
   if ($found == false) {
      echo ‘could not find ‘ . $valueToCheck;
   }
}
 
$valueToCheck = ‘chocolate’;
echo checkFor($valueToCheck);

So when we run this it will output:
could not find chocolate.

How do we debug this? A good approach is to examine all our variables to see if they are what we expect. We need to step through our code, starting from the bit that actually runs first. We know that line x is ok, and line x+1 seems ok too, since it passes the right variable to checkFor(). So let’s focus on what’s happening inside the function. Find the var_dump on line 2 and remove the 2 slashes before it to uncomment this line. Run the script again. Note that now it outputs the word ‘chocolate’ again, confirming that we are seeing the right data appear within our function. Now try uncommenting the var_dump on line 4 and running again. This will show us the structure of the array, and it should look fine (unless you’e made a typo!). Now try uncommenting the var_dump statement on line 7, and run again. Aha!

This post is part of PHP First Steps Tutorial. Go on to the next session: Project.

24 hr tutorial Hey!
Based on the feedback from readers of this tutorial series I’ve expanded, revised, and improved this brief intro as a whole book. It is much longer, so you get a more in-depth guide to getting started. As a result you’ll write better code. When you buy the book you also get a download password for all the source code from the tutorials. Build a functioning blog from scratch, and other useful mini-projects! Check it out »

No Comments

Be the first to start the conversation.

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>