PHP Loop - For, While, Do While And Foreach With Example

Objective

What is Loop Construct?
Types of Loops in PHP?
Understand Loop by Programming Example

WHAT IS LOOP CONSTRUCT IN PHP?

Loop means, repeating a job until condition fails. The loop allows you to iterate through list or array and allows you to execute a block of codes multiple times.

HOW DOES LOOP WORK?

There are 3 conditions in the loop.

1. Initialization: It is the starting point of a loop.
2. Termination: It is the point when loop gets terminated.
3. Increment/Decrement: It is the ratio to increment or decrements a loop.

HOW MANY TYPES OF LOOP IN PHP?

In PHP, there are 4 types of loop.
  1. For Loop
  2. While Loop
  3. Do While Loop
  4. Foreach Loop

FOR LOOP

For loop is used when you exactly know how many times of iterations are needed to complete a job. For example, you have a fixed array of 5 elements and you want to print arrays elements. Here, you know the size of an array so you can create a for loop that executes a block of code 5 times.

Syntax You can write for loop as follows:
for($i=0; $i<5; $i++)
{
//block of code
}
Here you can see that 3 conditions are specified in for loop.

$i=0 - It is starting point of a loop
$i<5 - It is ending point of a loop. When the value of $i reaches to 5, the loop gets terminated.
$i++ - It is the ratio of increment a loop. It means the value of $i gets incremented by 1 in each iteration.
The loop executes 5 times from 0 to 4.
0 1 2 3 4

Example
<?php
  $books=array("ASP.NET","PHP","Java","MySQL","C++");
  
  for($i=0; $i<5; $i++)
  {
   echo $books[$i]."<br />";
  }
?>
Output
ASP.NET
PHP
Java
MySQL
C++

WHILE LOOP

In the while loop, initialization is done before the while loop. Next, while loop checks for condition and if condition matches then while loop starts its execution.

Syntax

$i=1; //Initialization
While(condition) //Termination
{
//block of codes
$i++; //Increments
}
programming example
It this programming example, I will print all the even number before 29 using while loop.
<?php
$i=1;
while($i<=29)
{
 if($i%2==0) 
 {
 echo $i."<br />";
 }
 $i++;
}
?>
Output
2
4
6
8
10
12
14
16
18
20
22
24
26
28

DO WHILE LOOP

In Do While loop, the loop gets executed first, then it checks for condition and if condition matches then loop gets continued. The benefit of Do While loop is that it will surely execute block of code once.

Syntax

do {
   code to be executed;
}
while (condition);
Programming Example
The conditions are same as while loop.
<?php
$i=1;
do
{
 if($i%2==0) 
 {
 echo $i."<br />";
 }
 $i++;
}
while($i<29)
?>
Output
2
4
6
8
10
12
14
16
18
20
22
24
26
28

FOREACH LOOP

The foreach loop doesn't require initialization or termination. It works on list, collections, or arrays. It moves one by one element of array and terminates when reaches end of array or list.

Syntax:

foreach (array as value) {
   code to be executed;
}

Programming Example:
In this programming example, I will print an array value using foreach loop.
<?php
  $books=array("ASP.NET","PHP","Java","MySQL","C++");
  
  foreach($books as $b)
  {
   echo $b."<br />";
  }
?>

Output
ASP.NET
PHP
Java
MySQL
C++

Explanation:

In foreach loop, the element of array $books gets stored in variable $b and in the block of codes, this $b variable gets printed.

SUMMARY

In this tutorial, you learned about PHP Loop like for loop, while loop, do while loop and foreach loop with complete programming example. Loop constructs are very important in any programming languages and it is used widely. In the next chapter, you will learn about PHP Constants.