Open In App

PHP Loops

Last Updated : 30 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Like other programming languages, 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 explore the different types of loops in PHP, their syntax, and examples.

Why Use Loops?

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

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

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 for loop.

<?php 

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

?>

Output
1 2 3 4 5 

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 its 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
  
$num = 1;

while ($num <= 5) {
    echo $num . " ";
    $num++;
}

?>

Output
1 2 3 4 5 

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

$num = 1;

do {
    echo $num . " ";
    $num++;
} while ($num <= 5);

?>

Output
1 2 3 4 5 

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.

Syntax:

foreach ( $array as $value ) {
    // Code to be executed
}

// or

foreach ( $array as $key => $value ) {
    // Code to be executed
}

Example: Iterating through an array

<?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(
      "John" => 25, 
      "Alice" => 30, 
      "Bob" => 22
);

foreach ($ages as $name => $age) { 
    echo $name . " => " . $age . "\n";
}

?>

Output
10 20 30 40 50 60 
John => 25
Alice => 30
Bob => 22



Next Article

Similar Reads

three90RightbarBannerImg