English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP Loops

In this tutorial, you will learn how to use loops in PHP to repeat a series of operations.

Different types of loops in PHP

The loop can execute the same code block repeatedly as long as a specific condition is met. The basic idea of the loop is to automate repetitive tasks in the program to save time and effort. PHP supports four different types of loops.

  • while  —  Loop through the code block as long as the specified condition evaluates to true.

  • do…while  —  Execute the code block once, then evaluate the condition. If the condition is true, then as long as the specified condition is true, repeat the statement.

  • for  —  Loop through the code block until the counter reaches the specified number.

  • foreach  — Traversalthe code block for each element in the array.

You will also learn how to useforeach()Loop through the values of an array. The foreach() loop is specifically used for arrays.

PHP while loop

As long as the specified condition in the while statement evaluates to true, the statement will loop through the code block.

while(condition){ 
    //Code to be executed
}

The following example defines a loop starting with $i=1at the beginning of the loop. As long as $i is less than or equal to3 , the loop will continue to run. Each time the loop runs, the loop will increase $i1:

<?php
$i = 1;
while($i <= 3{
    $i++;
    echo "This number is " . $i . "<br>";
}
?>
Test and see‹/›

PHP do…while loop

-do-The while loop is a variant of the while loop, which evaluates the condition at the end of each iteration. By using do

do {
    //Code to be executed
}
while(condition);

The following example defines a loop with $i=1at the beginning of the loop. Then it will add $i by1, and then calculate the condition, as long as $i is less than or equal to3, the loop will continue to run.

<?php
$i = 1;
do{
    $i++;
    echo "This number is " . $i . "<br>";
}
while($i <= 3);
?>
Test and see‹/›

The difference between while and do…while loops

while loop with do-The while loop has an important difference: the while loop tests the condition to be evaluated at the beginning of each iteration, so if the result of the condition expression is false, the loop will never be executed.

On the other hand, for do-The while loop will always execute at least once, even if the condition expression is false, because the condition is evaluated at the end of the loop iteration rather than at the beginning.

PHP for loop

The for loop will repeat a block of code as long as a specific condition is met. It is usually used to execute a code block a certain number of times.

for(initialization; condition; increment){ 
    //Code to be executed
}

The parameters of the for loop have the following meanings:

  • initialization — it is used to initialize the counter variable and is calculated unconditionally before the first execution of the loop body.

  • condition — it is evaluated at the beginning of each iteration. If the result is true, the loop continues and executes the nested statements. If the result is false, the execution of the loop ends.

  • increment — it updates the loop counter with the new value. It is calculated at the end of each iteration.

The following example defines a loop with $i=1Starting the loop. This loop will continue until $i is less than or equal to3. The variable $i will increment each time the loop runs1:

<?php
for($i=1; $i<=3; $i++{
    echo "This number is " . $i . "<br>";
}
?>
Test and see‹/›

PHP foreach loop

The foreach loop is used to traverse arrays.

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

The following example demonstrates a loop that prints the values of the given array:

<?php
$colors = array("Red", "Green", "Blue");
 
//Traverse the colors array
foreach($colors as $value){
    echo $value . "<br>";
}
?>
Test and see‹/›

There is another syntax for the foreach loop, which is an extension of the first one.

foreach($array as $key => $value){ 
    //Code to be executed
}
<?php
$superhero = array(
    "name" => "Peter Parker",
    "email" => "[email protected]",
    "age" => 18
);
 
//Traverse the $superhero array
foreach($superhero as $key => $value){
    echo "$key ": ". $value . "<br>";
}
?>
Test and see‹/›