Loops

Main | Static vs. Dynamic Websites | Using PHP | A Simple PHP Program | Variables and Operators | Practice 1 | Solution 1 | Loops | Practice 2 | Solution 2 | Functions | Practice 3 | Solution 3 | Conditionals | Practice 4 | Solution 4

Computers often have to do the same task repetitively until some condition is satisfied. PHP allows you to write programs that do this by using a loop. The idea of loop can be illustrated with a simple example, which you can view in a new browser window by clicking here.

Repeating a message so many times might seem like a useless feature of PHP at first, but the above example was only used to illustrate what a loop is. In future examples of this tutorial, PHP loops will be used to display the contents of an online MySQL database. We will also use PHP loops to automate the repetitive task of coding HTML tables. PHP loops help bring the power of automation to the web, and without them many of the websites that utilize dynamic content would not be possible.

A loop can be thought of as something you wrap around your PHP code, to make it happen again and again. The code for the above example is the following.

<html>
<? 
for($i = 0; $i < 100; $i++) { 
   echo "this message has echoed $i times <br>"; 
} 

?>
</html>

Note how in the above example that the statement within the curly braces ({ and }) is indented. This practice of indenting code is part of a programming standard that promotes readability. If you do not indent your code within curly braces the program will run, but it will make the code appear untidy. The code that you write may be left for others to read, especially if you are working on a large program with other programmers, or if you ask others for help. In either case they will expect you to indent your code according to the standard so that they will find it easy to read. The standard for PHP is to be sure that whenever you are within a set of curly braces ({ and }) that you indent each line of code that is in between them.

We will now examine a general version the above loop, without PHP's HTML embedding tags. This example is a for loop (we will discuss while loops soon). The best way to understand the syntax of a loop is to go through it as if you were a computer. For this reason the following analysis may seem somewhat technical. However, a detailed explanation will be necessary to help you understand the loop's syntax, so you should reread the steps below and relate them to the following code until you have an thorough understanding.

for($i = 0; $i < 100; $i++) { 
  some php code
} 

  1. When PHP parses the above code, it first sees for and looks inside the parentheses for three statements, separated by semicolons. The first statement that it parses is the declaration of the variable $i (remember in PHP variables must be preceded by a dollar sign) being set equal to zero by the statement $i = 0;.

  2. It then looks at the middle statement to check if the condition is true. In other words, it looks to see if $i < 100.

  3. Since $i is equal to 0 at this point, PHP goes on to the next statement, $i++, which means, raise the value of $i by one.

  4. PHP would then go on to execute whatever statements were made between the curly braces ({ and }), as many times as necessary, until the statement $i < 100 became false.

  5. Since $i was first set to zero, the code between the curly braces would be executed 100 times. If we wanted the loop to iterate more (or less) than 100 (0-99) times, we would replace 100 with a different value.

We will now look at a way to write loops with a different syntax. If you understand the above, you should find it easy to write while loops. Here is an example of the generic code above, rewritten to use a while loop:

$i = 0;
while ($i < 100) {
    some php code
    $i++;
}

The loop begins by checking if $i < 100. If this condition fails, then the loop is entered and some php code is executed. You should have a statement in your while loop which will eventually cause the condition under which it runs ($i < 100) to be false. If you don't do this, the loop will never stop and your program will appear "frozen". In this case the statement $i++; will increment $i one hundred times until it is false that $i < 100 and the loop will stop.

Practice 2 will test your understanding of the above, and show you a simple application relating to the automation of HTML.


jfulton [at] member.fsf.org
22 Aug 2013