Logic in PHP: Repeating Yourself

When a computer program does something repeatedly, it’s called looping. This hap­pens a lot—for example, when you want to retrieve a set of rows from a database, print rows of an HTML table, or print elements in an HTML <select> menu. The two looping constructs discussed in this section are while() and for(). Their specifics differ, but each requires you to specify the two essential attributes of any loop: what code to execute repeatedly and when to stop. The code to execute is a code block just like what goes inside the curly braces after an if() construct. The condi­tion for stopping the loop is a logical expression just like an if() construct’s test expression.

The while() construct is like a repeating if(). You provide an expression to while(), just like to if(). If the expression is true, then a code block is executed. Unlike if(), however, while() checks the expression again after executing the code block. If it’s still true, then the code block is executed again (and again, and again, as long as the expression is true.) Once the expression is false, program execution continues with the lines after the code block. As you have probably guessed, your code block should do something that changes the outcome of the test expression so that the loop doesn’t go on forever.

Example 3-17 uses while() to print an HTML form <select> menu with 10 choices.

Example 3-17. Printing a <select> menu with while()

$i = 1;

print ‘<select name= “people”>’; while ($i <= 10) {

print “<option>$i</option>\n”;



print ‘</select>’;

Example 3-17 prints:

<select name= “people”><option>1</option>











Before the while() loop runs, the code sets $i to 1 and prints the opening <select> tag. The test expression compares $i to 10. As long as $i is less than or equal to 10, the two statements in the code block are executed. The first prints an <option> tag for the <select> menu, and the second increments $i. If you didn’t increment $i inside the while() loop, Example 3-17 would print out <option>1</option> forever.

After the code block prints <option>10</option>, the $i++ line makes $i equal to 11. Then the test expression ($i <= 10) is evaluated. Since it’s not true (11 is not less than or equal to 10), the program continues past the while() loop’s code block and prints the closing </select> tag.

The for() construct also provides a way for you to execute the same statements mul­tiple times. Example 3-18 uses for() to print the same HTML form <select> menu as Example 3-17.

Example 3-18. Printing a <select> menu with for()

print ‘<select name= “people”>’;

for ($i = 1; $i <= 10; $i++) {

print “<option>$i</option>\n”;


print ‘</select>’;

Using for() is a little more complicated than using while(). Instead of one test expression in parentheses, there are three expressions separated with semicolons: the initialization expression, test expression, and iteration expression. Once you get the hang of it, however, for() is a more concise way to have a loop with easy-to-express initialization and iteration conditions.

The first expression in Example 3-18, $i = 1, is the initialization expression. It is eval­uated once, when the loop starts. This is where you put variable initializations or other setup code. The second expression in Example 3-18, $i <= 10, is the test expression. It is evaluated once each time through the loop, before the statements in the loop body. If it’s true, then the loop body is executed (print “<option> $i</option>”; in Example 3-18). The third expression in Example 3-18, $i++, is the iteration expression. It is run after each time the loop body is executed. In Example 3-18, the sequence of statements goes like this:

  1. Initialization expression: $i = 1;
  2. Test expression: $i <= 10 (true, $i is 1)
  3. Code block:print “<option>$i</option>“;
  4. Iteration expression: $i++;
  5. Test expression: $i <= 10 (true, $i is 2)
  6. Code block:print “<option>$i</option>”;
  7. Iteration expression: $i++;
  8. (Loop continues with incrementing values of $i)
  9. Test expression: $i <= 10 (true, $i is 9)
  10. Code block:print “<option>$i</option>“;
  11. Iteration expression: $i++;
  12. Test expression: $i <= 10 (true, $i is 10)
  13. Code block:print “<option>$i</option>“;
  14. Iteration expression: $i++;
  15. Test expression: $i <= 10 (false, $i is 11)

You can combine multiple expressions in the initialization expression and the itera­tion expression of a for() loop by separating each of the individual expressions with a comma. This is usually done when you want to change more than one variable as the loop progresses. Example 3-19 applies this to the variables $min and $max.

Example 3-19. Multiple expressions in for()

print ‘<select name= “doughnuts”>’;

for ($min = 1, $max = 10; $min < 50; $min += 10, $max += 10) {

print “<option>$min – $max</option>\n”;


print ‘</select>’;

Each time through the loop, $min and $max are each incremented by 10. Example 3-19 prints:

<select name= “doughnuts”><option>1 – 10</option>

<option>11 – 20</option>

<option>21 – 30</option>

<option>31 – 40</option>

<option>41 – 50</option>


Source: Sklar David (2020), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *