Open In App

PHP Loops

Last Updated : 10 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, Loops are used to repeat a block of code multiple times based on a given condition. PHP provides several types of loops to handle different scenarios, including while loops, for loops, do…while loops, and foreach loops.

In this article, we will discuss the different types of loops in PHP, their syntax, and examples.

Types of Loops in PHP

Below are the following types of loops in PHP:

  • for loop
  • while loop
  • do-while loop
  • foreach loop

1. PHP for Loop

PHP for loop is used when you know exactly how many times you want to iterate through a block of code. It consists of three expressions:

  • Initialization: Sets the initial value of the loop variable.
  • Condition: Checks if the loop should continue.
  • Increment/Decrement: Changes the loop variable after each iteration.

Syntax

for ( Initialization; Condition; Increment/Decrement ) {
// Code to be executed
}

Example: Printing numbers from 1 to 5 using a for loop.

PHP
<?php 

// Code to illustrate for loop
for ($num = 1; $num <= 5; $num += 1) {
    echo $num . "\n";
} 

?>

Output
1
2
3
4
5

2. PHP while Loop

The while loop is also an entry control loop like for loops. It first checks the condition at the start of the loop, and if it’s true then it enters into the loop and executes the block of statements and goes on executing it as long as the condition holds true.

Syntax

while ( condition ) {
// Code is executed
}

Example: Printing numbers from 1 to 5.

PHP
<?php
$num = 1;
while ($num <= 5) {
    echo $num . "\n";
    $num++;
}
?>

Output
1
2
3
4
5

3. PHP do-while Loop

The do-while loop is an exit control loop, which means, it first enters the loop, executes the statements, and then checks the condition. Therefore, a statement is executed at least once using the do…while loop. After executing once, the program is executed as long as the condition holds true.

Syntax

do {
// Code is executed
} while ( condition );

Example: Printing numbers from 1 to 5.

PHP
<?php
$num = 1;
do {
    echo $num . "\n";
    $num++;
} while ($num <= 5);
?>

Output
1
2
3
4
5

4. PHP foreach Loop

This foreach loop is used to iterate over arrays. For every counter of loop, an array element is assigned, and the next counter is shifted to the next element. It simplifies working with arrays and objects by automatically iterating through each element.

Syntax:

foreach ( $array as $value ) {
// Code to be executed
}
or
foreach ($array as $key => $value) {
// Code to be executed
}
  • $array: The array to iterate over.
  • $value: The current value of the array element during each iteration.

Example: Iterating through an array

PHP
<?php
// foreach loop over an array  
$arr = array (10, 20, 30, 40, 50, 60);
foreach ($arr as $val) { 
	echo $val . " ";
}
echo "\n";
// foreach loop over an array with keys
$ages = array(
  	"Anjali" => 25, 
  	"Kriti" => 30, 
  	"Ayushi" => 22
);
foreach ($ages as $name => $age) { 
    echo $name . " => " . $age . "\n";
}
?>

Output
10 20 30 40 50 60 
Anjali => 25
Kriti => 30
Ayushi => 22

Why Use Loops?

Loops allow you to execute a block of code multiple times without rewriting the code. This is useful when working with repetitive tasks, such as:

  • Iterating through arrays or data structures
  • Acting a specific number of times
  • Waiting for a condition to be met before proceeding

Conclusion

Loops are an important feature in PHP that help developers repeat tasks automatically. Whether you need to go through items in an array, run a block of code multiple times, or wait until a condition is true, PHP offers different types of loops to make these tasks easier and more efficient.



Next Article

Similar Reads