PHP Loops are used to execute a block of code multiple times based on a given condition. Explore the types of PHP Loops – for, while & foreach loops with syntax and examples:
In this tutorial, you will learn what loops are, the types of loops (PHP while, PHP do while, PHP for, & PHP foreach), differences between while & do-while, differences between for & foreach, and frequently asked questions (FAQs). Please note that we have used PHP version 7 in all examples.
Let’s begin!
Table of Contents:
Introduction to PHP Loops

The function of a loop is to execute the same code block a specified number of times. It is also referred to as a repetition statement. Furthermore, it can be nested too.
Types of Loops in PHP
There are four loops in PHP, as shown in the list below:
- While
- Do-while
- For
- Foreach
PHP While Loop
PHP while loop iterates through a code block as long as the specified condition is true. It is also the simplest type.
Syntax:
while(condition){
//code to be executed
}
Alternatively, you may use the following syntax.
while(condition):
//code to be executed
endwhile;
A few examples are shown below. You can practise these examples by running the following programming code.
Example 1:
<?php
$num = 1;
while($num <= 3){
echo "Hello! <br/>";
$num++;
}
?>
Output:
Hello!
Hello!
Hello!
Note: In the above example, there will be no output if the specified condition is false at the very beginning.
Example 2:
<?php
$num = 1;
while($num <= 5){
echo "$num <br/>";
$num++;
}
?>
Output:
1
2
3
4
5
Example 3:
<?php
$num = 8;
while($num <= 5){
echo "$num <br/>";
$num++;
}
?>
The above programming code does not have any output as the specified condition is false at the very beginning.
PHP Do While Loop
PHP do-while iterates through a code block once and then continues the loop as long as the specified condition is true. The do-while loop is guaranteed to execute a minimum of once.
Syntax:
do{
code to be executed;
}while (condition);
A few examples are shown below. You can practise these examples by running the following programming code.
Example 1:
<?php
$num = 1;
do{
echo "$num<br>";
$num++;
}while($num <= 5);
?>
Output:
1
2
3
4
5
Example 2:
<?php
$num = 5;
do{
echo "$num<br>";
$num--;
}while($num >= 1);
?>
Output:
5
4
3
2
1
Example 3:
<?php
$num = 20;
do{
echo "$num<br>";
$num++;
}while($num <= 5);
?>
Output:
20
While vs Do-while
Both while and do-while are similar to each other. However, there are some differences too.
The major difference between while and do-while is that do-while always executes once, even if the specified condition is false. But while loop evaluates the specified condition at the beginning and only executes if the condition is true.
The table below shows the difference between while and do-while.
| While | Do-while | |
| 1 | Evaluate the condition before entering into the loop body (at the beginning of the loop). | Evaluate the condition when coming out of the loop (at the end of the loop). |
| 2 | Executes zero or more times. | Executes one or more times. |
| 3 | The condition should be initialized at the beginning. | The condition need not be initialized at the beginning. |
PHP For Loop
PHP for loop iterates through a code block a specified number of times.
Syntax:
for(init counter; test counter; increment/decrement counter){
code to be executed
}
An example is given below. You can practise this example by running the following programming code.
<?php
for($num = 1; $num <= 5; $num++){
echo "$num<br>";
}
?>
Output:
1
2
3
4
5
PHP Foreach Loop
PHP foreach iterates through a code block for each element in an array data structure.
Syntax:
foreach($array as $value){
code to be executed;
}
Alternatively, you may use the following syntax (please see example 2 below).
foreach ($array as $key => $value) {
code to be executed;
}
A few examples are shown below. You can practise these examples by running the following programming code.
Example 1:
<?php
$value = array("HTML", "CSS", "JS");
foreach($value as $val){
echo "$val <br>";
}
?>
Output:
HTML
CSS
JS
Example 2:
<?php
$signals = array("Red"=>"Stop!", "Yellow"=>"Wait!", "Green"=>"Go!");
foreach($signals as $sign => $value){
echo "$sign = $value<br>";
}
?>
Output:
Red = Stop!
Yellow = Wait!
Green = Go!
For vs Foreach
Both for and foreach are similar to each other. But, there are significant differences too.
The below table shows the difference between for and foreach.
| For | Foreach | |
| 1 | Works on variables. | Works on arrays. |
| 2 | Iterates the code block a specified number of times. | Iterates over the array elements. |
| 3 | Does not hide the iteration. | Hides the iteration. |
| 4 | Comparatively complex. | Comparatively simple. |
| 5 | Comparatively takes more time to execute. | Comparatively takes less time to execute. |
Break and Continue Keywords
The Break Keyword
We have already covered the break keyword in our previous tutorial. However, we are going to look at how the break keyword can be used in loops.
The break keyword is used to immediately end the execution of a loop.
In a nested loop, the break keyword ends the execution of the loop where the break is placed. If the break keyword is placed in the inner loop, then the outer loop still can be executed. But if the break is placed in the outer loop, then it will stop the outer loop as well as the inner loop(s).
Let’s see a few examples. You can practise these examples by running the following programming code.
Example 1:
<?php
for($num = 1; $num < 5; $num++){
if($num == 3){
break;
}
echo "Number $num <br>";
}
?>
Output:
Number 1
Number 2
Example 2:
<?php
$num = 1;
while($num < 5){
if($num == 3){
break;
}
echo "Number $num <br>";
$num++;
}
?>
Output:
Number 1
Number 2
The Continue Keyword
The continue keyword is used to go to the next iteration of a loop.
Let’s see a few examples. You can practise these examples by running the following programming code.
Example 1:
<?php
for($num = 1; $num < 5; $num++){
if($num == 3){
continue;
}
echo "Number $num <br>";
}
?>
Output:
Number 1
Number 2
Number 4
Example 2:
<?php
$num = 1;
while($num < 5){
if ($num == 3){
$num++;
continue;
}
echo "Number $num <br>";
$num++;
}
?>
Output:
Number 1
Number 2
Number 4
Frequently Asked Questions (FAQs)
1. What are the four types of loops?
They are while, do-while, for, and foreach
2. What is a PHP for loop?
It is a loop that iterates through a code block a specified number of times.
3. What is PHP foreach?
It is a loop that iterates through a code block for each element in an array data structure.
4. Can we use break inside a PHP while loop?
Yes, we use a break inside a while loop.
5. How many times does a PHP while loop guarantee to loop?
It is guaranteed to loop at least once. That means even if the specified condition is false, it will execute at least once.
6. Does a break stop all loops in a nested loop?
No, unless the break is placed in the outer loop.
Conclusion
A loop or a repetition statement executes the same code block a specified number of times. There are four loops in PHP: while, do-while, for, and foreach.
The break keyword is used to immediately end the execution of a loop, while the continue keyword is used to go to the next iteration of a loop.






