Lesson Weekend

Lesson goals:

  • Learn to use while loops.
  • Learn to use increment and decrement operators.

Let's add another type of loops to your arsenal, called while loops. To do this, we'll make a timer in the PHP shell. We will enter a number of seconds and then our program will print out the current count, decreasing it by one each time, until it reaches 0. We can do this with a while loop. Let's say we want 3 seconds to be counted. Type this into a text file and then paste it into the PHP shell.

$seconds = 3;
while ($seconds > 0) {
    echo $seconds . " ";
    sleep(1);
    $seconds = $seconds - 1;
}

while loops are very similar to if statements

When the PHP interpreter comes across the keyword while, it expects a pair of parenthesis to come next with a condition inside of them. If that condition evaluates to true, then the code inside of the two curly brackets is run. But then, the condition is re-evaluated. If it is still true (in our example, if $seconds is still greater than 0) then the code in the curly brackets is run a second time. This continues until that condition next to the while keyword evaluates to false. Then the program continues on past the end of the curly brackets.

Let's step through the while loop we did above.

  1. Set the number of seconds equal to 3.
  2. Is $seconds greater than 0? Since 3 > 0, the condition is true.
  3. Use echo to print the current value of $seconds with a space added onto the end of it.
  4. Use the sleep function with an argument of 1 to make the PHP interpreter wait for 1 second before continuing to the next line.
  5. Set the value of $seconds to the current value of $seconds minus 1.
  6. Check the condition again. $seconds is equal to 2 now, which is still greater than 0, so the condition evaluates to true and the code in curly brackets is executed again.
  7. Print out the current value of $seconds, which is still 2. Then sleep for one second.
  8. Subtract 1 from $seconds.
  9. Check the condition again. $seconds is 1, so it is still true.
  10. Run the code in the curly brackets again, first printing the value of $seconds, sleeping for one second, then decrementing by 1 again.
  11. Now check the condition again. $seconds is equal to 0, which is not greater than 0. The condition is false, so the code in the curly brackets does not run.

Remember how our code travels from top to bottom like a car driving down the highway? When it encounters the if keyword, the car has come to a fork in the road and it has to choose one direction to travel in based on whether one or more conditions are true or false.

But a while loop is more like encountering a cul de sac. Except, imagine that there is a police officer at the entrance. She informs us that we're on a one way street and we have to enter the cul de sac. She also says we can't leave the cul de sac until traffic is clear - this is the while condition. While there is traffic, we can't leave. In code it would look like this:

while ($traffic) {
    echo "We're stuck in a loop!"
}

If $traffic is truthy, then the loop runs. So as the loop runs, in our metaphor, we drive around the cul de sac in a circle (executing all the code in the curly brackets once). When we come back to the same cop at the entrance we check the condition by asking if traffic has cleared yet. If the condition is still true, and there is traffic, then she'll say we have to loop around one more time. But eventually, if we keep circling the block we'll be allowed to leave once the condition is false.

This brings up an important point with while loops. You have to have something in the curly brackets code which changes the values in the condition so that it will at some point evaluate to false. Otherwise the loop will continue endlessly, until you quit out of your frozen browser. In our metaphor, if traffic never clears, we will be stuck in that cul de sac forever!

So in our timer program, we subtract one from the current count each time the loop runs. This way, $seconds will eventually get to 0, and our condition will be false.

$seconds = $seconds - 1;

There are two little operators used for incrementing and decrementing a variable by 1 because you have to do it so often. They are -- (two minus signs) and ++ (two plus signs), and they go immediately to the left of any integer or float variable, with no spaces.

--$seconds;    // Subtract one from $seconds and store the new value in $seconds.
$seconds = $seconds - 1;  // Same as above.
++$seconds;  // Add one to $seconds and store the new value in $seconds.
$seconds = $seconds + 1; // Same as above.

Here is an example of a while loop. The code inside of the curly brackets will repeat as long as the condition is true.

$seconds = 3;
while ($seconds > 0) {
    echo $seconds . " ";
    --$seconds;
}

This will print:

3 2 1 

The increment and decrement operators are used all the time in looping (and elsewhere) to add or subtract 1 to a variable.

Tell a variable to add 1 to itself: ++$variable; Tell a variable to subtract 1 from itself: --$variable;