Iteration in PHP
Tags: php, todayilearned, beginners
Published: 2019-06-17 12:31:57

Most times when we write code, we usually want a block of code to run over and over again in a row. Instead of adding several almost equal lines of code-lines in a script, we can use loops to perform such a task.

#The PHP while loop#
The while loop executes a block of code as long as the specified condition is true.

the syntax is
while (condition is true) {
    code to be executed;
}
Example:
```
$x = 1; 
while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
```
The example above first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++):

#The PHP do...while Loop#
The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.
The syntax is: 
do {
    code to be executed;
} while (condition is true);

Example:
```
$x = 1; 
do {
    echo "The number is: $x <br>";
    $x++;
} while ($x <= 5);
```
The example above first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5.
 We should note that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time.

#PHP 7 for Loops#
 The for loop is used when you know in advance how many times the script should run.
 The syntax is:
for (init counter; test counter; increment counter) {
    code to be executed;
}
the parameters are:
init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration.If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value.
The example below displays the numbers from 1 to 10
```
for ($x = 1; $x <= 10; $x++) {
    echo "The number is: $x <br>" ;
}
```
#The PHP foreach Loop#
The foreach loop works only on arrays and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) {
    code to be executed;
}
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 demonstrates a loop that will output the values of the given array ($cars):
```
$cars = array("toyota", "bmw", "ford", "lexus"); 
foreach ($cars as $value) {
    echo "$value <br>";
}
```
#The PHP nested loop#
 Php allows putting one loop inside another, this type of loop is known as a nested loop. A nested loop can be a forloop, while loop or even foreach loop. Syntax of a nested while-loop is:

while(condition)
{
    while(condition)
     {
        statement(s);
        increment/decrements;
     }
    statement(s);
    increment/decrements;
}

an example is:
```
 $i = 1 ;
 $j = 1;
while( $i < 3 )
{
    while( $j < 3 )
    {
        echo 'i love programming ';
        $j++;
    }
    echo '<br />';
    $i++;
}
```
The syntax of a forloop nested loop is:

for ( initialization; condition; increment/decrements )
{
   for ( initialization; condition; increment/decrements )
   {
      statement(s);
   }
   statement(s);
}
An example is:
```
 $i ;
 $j;
for( $i=1; $i<3; $i++ )
{
   for( $j=1; $j<3; $j++)
   {
      echo 'i love php';
   }
   echo '<br/>';
}
```
The condition of the outer loop needs to be TRUE before the inner loop can run and more than two loops can be nested.