PHP Loops

This tutorial enables you to learn how to use PHP Loops. So, let us begin.

 

What is PHP Loop?

You can execute something over and over again by using the loop.

For example, for displaying all the numbers from 1 to 1000, you can just use a loop instead of using an echo statement 1000 times or specifying all the numbers in a single echo statement with newline character n. This is because a loop will run for 1000 times, and every time a number is displayed, starting from 1, the number is incremented after each iteration or cycle.

In a Loop, you either specify a condition or a LIMIT up till which the loop will execute, the reason being if such a condition is not specified, then it would be difficult to specify when the loop should end and not run for infinite time.

 

 

PHP while Loop

There are two components of the while loop in PHP. These are the condition and the code to be executed. The PHP while loop will execute the given code until the specified condition is true.

Syntax:

<?php 

while(condition) { 

/* execute this code till the condition is true */ 

?>

 

Example:

<?php

$a = 1;

while($a <= 10)
{
    echo "$a | ";
    $a++;   // incrementing value of a by 1
}
?>

Output:

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |

 

PHP do...while Loop

It is a little different from all the loops in PHP. The reason is PHP do...while loop will execute at least one time, even in case the condition is false. Do you know how? Well, because it will check the condition after the loop's execution. Hence, the loop has already been executed once when the condition is checked.

Syntax:

<?php 

do { 

/* execute this code till the condition is true */ 

} while(condition) 

?>

 

Example:

<?php

$a = 1;

do {
    echo "$a | ";
    $a++;   // incrementing value of a by 1
} while($a <= 10)
?>

Output:

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |

 

Let us consider another example where the loop will be executed once even if the condition is false.

<?php

$a = 11;

do {
    echo $a;
    $a++;   // incrementing value of a by 1
} while($a <= 10)
?>

Output:

11

As we can see, the condition in the above do...while loop example will return false because the $a variable's value is 11. So, according to the condition, the loop should be executed only if $a's value is less than or equal to 10.

 

PHP for Loop

The PHP for loop works differently from while or do...while loop. In for loop case, we have to declare beforehand how many times we want the loop to run.

Syntax:

<?php 

for(initialization; condition; increment/decrement) 

/* execute this code till the condition is true */ 

?>

The meaning of for parameters used is as follows:

  • initialization: In it, a variable is initialized with some value- the variable acts as the loop counter.
  • condition: The condition which is checked after each iteration/cycle of the loop is defined here. The loop is executed only in case the condition returns true.
  • increment/decrement: The loop counter is incremented or decremented as per the requirements.

 

 

So, let us try printing numbers from 1 to 10 using the for loop.

<?php

for($a = 1; $a <= 10; $a++)
{
    echo "$a <br/>";
}
?>

Output:

1
2
3
4
5
6
7
8
9
10

 

Nested for Loops

A for loop can also be used inside another for loop. Here is a simple example of nested for loops.

<?php

for($a = 0; $a <= 2; $a++)
{
    for($b = 0; $b <= 2; $b++)
    {
        echo "$b $a ";
    }
}

?>

Output:

0 0
1 0
2 0
0 1
1 1
2 1
0 2
1 2
2 2

 

PHP foreach Loop

You can use the foreach loop in PHP to access an array's key-value pairs. The foreach loop only works with arrays. You do not have to initialize any loop counter or set any condition for exiting from the loop; the loop does everything implicitly(internally).

Syntax:

<?php 

foreach($array as $var) { 

/* execute this code for all the array elements 

$var will represent all the array 

elements starting from first element, one by one */ 

}

?>

 

Example:

<?php

$array = array("Jaguar", "Audi", "Mercedes", "BMW");

foreach($array as $var)
{
    echo "$var <br/>";
}

?>

Output:

Jaguar
Audi
Mercedes
BMW