PHP Article

Looping

This article will explain the different types of looping;
while loop, do...while loop, for loop, foreach loop, and how to exit a loop with break/continue .

This article assumes you have basic knowledge of HTML and PHP.

while loop

                    
<?php
$x = 1;

while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
}
?>

OUTPUT:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5                    
                

In the example above

  • $x = 1; - Starts the loop counter with a value of 1
  • $x <= 5 - continues the loop as long as $x is less than or equal to 5
  • $x++; - increments the loop counter by 1
We assign a value of 1 to x and start the counter with the value of x. You can asign any number as long as it is smaller than the number to the right. We write the line and increment the counter and keep wrting as long as the statement is true.

do...while loop

The do...while loop - Loops through a block of code once, and then repeats the loop as long as the specified condition is true.
It will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

                    
<?php
$x = 1;

do {
  echo "The number is: $x <br \>";
  $x++;
} while ($x <= 5);
?>

OUTPUT:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5                    
                

The do..while loop is very much like the while loop except in a do...while loop the condition is tested AFTER executing the statements within the loop. This means that the do...while loop will execute its statements at least once, even if the condition is false. You could change the value of x to 6 and it would print "The number is 6" before exiting the loop.

for loop

In the example below

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 10; - continue the loop as long as $x is less than or equal to 10
  • $x++ - increments the loop counter value by 1 for each iteration

                    
<?php
for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x <br />";
}
?>

OUTPUT:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10                    
                

If we want to count by steps, lets say we want to count to 100 by 10s then we would follow the example below.
Where:

  • $x <= 100; - continue the loop as long as $x is less than or equal to 100
  • $x+=10 - increments the loop counter value by 10 for each iteration

                    
<?php
for ($x = 0; $x <= 100; $x+=10) {
  echo "The number is: $x <br />";
}
?>

OUTPUT:
The number is: 0
The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50
The number is: 60
The number is: 70
The number is: 80
The number is: 90
The number is: 100                    
                

foreach loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

The following example will output the values of the given array ($colors):

                    
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value <br>";
}
?>

OUTPUT:
red
green
blue
yellow                    
                

The following example will output both the keys and the values of the given array ($age):

                    
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {
  echo "$x = $val<br />";
}
?>

OUTPUT:
Peter = 35
Ben = 37
Joe = 43                    
                

Finally the break and continue statements

The break statement is be used to jump out of a loop.

The example jumps out of the loop when x is equal to 4:

                    
<?php
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br />";
}
?>

OUTPUT:
The number is: 0
The number is: 1
The number is: 2
The number is: 3                    
                

The continue statement skips one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
This example skips the value of 4:

                    
<?php
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    continue;
  }
  echo "The number is: $x <br />";
}
?>

OUTPUT:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9                    
                

I hope have explained it well. Happy Coding!