Lesson 8: PHP for loops and conditions

ByDr.-Ing. Erik Neitzel
Lesson 8: PHP for loops and conditions

This lesson shows step-by-step how to use PHP for loops and conditions within dynamic web pages. Those control structures are hugely important to generate dynamic content. Think about how WordPress is creating a list of all the posts you have in your database. All the HTML code that is needed to show that list is not written by hand, but generated by a PHP loop. To stick with that example, let us talk about loops.

Implementing loops in PHP

Why loops? Whenever we need to either repeat certain operations for a known or unknown amount of times, we are going to need loops. Loops can make it easier to just repeat the same output without having to copy/paste it several times, which makes it easier to maintain the code. The most important reason for loops, however, is when we want to dynamically build output based on certain conditions.

An example is building a table with a known number of columns and an unknown number of rows, whereas rows are to be repeated for as long as a database backend has data sets left for an entity. We will discuss database connectivity at a later time. For now, let’s settle on learning how to use basic loops in general.

WHILE Loops in PHP

A loop often used for different types of conditions is the while loop. To implement it, simple use the word while, with a condition in brackets, and the code to be repeated within the loop’s content area.

1
2
3
4
5
6
7
8
<?php
     $t = "I will never forget the semicolon at the end of a line<br />";
     $i = 0;
     while ($i < 10) {
          echo $t;
          $i++;
     }
?>

Please note that it is essential to count the amount of times your loop went through a cycle. You do that using an incrementation counter, as seen in the code above. If you don’t, your loop will run indefinitely.

DO WHILE Loops in PHP

Another method of using while loops is the do…while approach. It is the same as above, but the loops code will always be executed at least one time, and the condition is checked afterwards.

1
2
3
4
5
6
7
<?php
     $i = 0;
     do {
          echo "There will be text.";
          $i++;
     } while ($i < 10);
?>

FOR Loops in PHP

A different type of loop is the for loop. It needs three terms or expressions:

  1. The initialization term. It will set the initial or start value of your counter variable.
  2. The abort condition. It compares the current counter value with the maximum value.
  3. The loop operation. It increments or decrements the counter value.

The following code shows the right syntax of a for loop:

1
2
3
4
5
6
<?php
     $t = "I will be repeated for some time.<br />"
     for ($i = 0; $i < 10; $++) {
          echo $ti;
     }
?>

A for loop is a pure counting loop and cannot run indefinitely, however you should pay attention to the terms you provide it with.

Conditions in PHP

We spoke about a counter condition for several times. However there are also other conditions which a while loop would accept and even need in some cases. Let’s look at some basic conditions:

If … else

The most rudimentary form of condition is the if…else condition. It separates different cases, its syntax is as follows:

1
2
3
4
5
6
7
<?php
     if ($i < 0) {
          echo "$i is smaller than 0, promised.<br />";
     } else {
          echo "I assure you, $i is currently either 0 or greater than 0.<br />";
     }
?>

Of course you could also put more than one if clause to the chain:

1
2
3
4
5
<?php
     if ($i > 10) { ... }
     else if ($i < 5 ) { ... }
     else { ... }
?>

Please note that “else if” will only be executed if the first condition is wrong. If you want to have another condition checked at all times, do so as follows:

1
2
3
4
5
<?php
     if ($i > 5) { ... }
     if ($i > 10) { ... }
     if ($i > 20) { ... }     
?>

You can also specify more than one condition at the same time using either && (AND) or || (OR):

1
2
3
4
<?php
     if ($i == 5 && $j != 10) { ... }  
     if ($i == 0 || $j == 0) { ... }  
?>

We will cover the comparison operations “==”, “!=” etc. in a second.

Finally, you could also use another if clause within the execution space of another one:

1
2
3
4
5
6
7
8
<?php
     if ($i == 5) {
          if ($j == 10) {
               ...
          } else { ... }
     }  
     else { ... }
?>

That way you can implemented different types of fallback scenarios for each case.

SWITCH … CASE selections

If you have just one variable which you expect to have different known values at times, the easiest way to differentiate between those cases is to switch between those cases:

1
2
3
4
5
6
7
8
9
10
11
<?php
     switch ($name) {
          case "Mike": 
               echo "I am Mike.<br />";
               break;
          case "Annie": 
               echo "I am Annie.<br />";
               break;
          default: 
               echo "We are the rest.";
?>

Comparison operations in PHP

We already used smaller than “<" and greater than ">” operations. However there are way more comparisons possible:

  • $i == 10: equals
  • $i != 10: not equals
  • $i >= 10: greater or equals
  • $i <= 10: smaller or equals
  • $i == 10 && $j == 11: logical AND combination of two conditions
  • $i == 10 || $j == 12: logical OR combination of two conditions

These are all basic operations you will need to implement loops, differentiate between cases and define the conditions you need.

The next lessons:

Dr.-Ing. Erik Neitzel