Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn PHP Loops



In the following chapters you will learn how to repeat code by using loops in PHP.


PHP While Loop

The while loop executes a block of code as long as the specified condition is true.
Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:



The following chapters will explain and give examples of each loop type.



PHP while Loop


The while loop executes a block of code as long as the specified condition is true.


The while loop executes a block of code as long as the specified condition is true.


Example


           $i = 1;
            while ($i < 6) {
              echo $i;
              $i++;
            }

Note: remember to increment $i, or else the loop will continue forever.


The while loop does not run a specific number of times, but checks after each iteration if the condition is still true.
The condition does not have to be a counter, it could be the status of an operation or any condition that evaluates to either true or false.



The break Statement


The break statement can also be used to jump out of a loop.


Example

Stop the loop when $i is 3:


           $i = 1;
            while ($i < 6) {
              if ($i == 3) break;
              echo $i;
              $i++;
            }



The continue Statement


The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues

Example

Stop, and jump to the next iteration if $i is 3:


           hile ($i < 6) {
              $i++;
              if ($i == 3) continue;
              echo $i;
            }


Alternative Syntax


The while loop syntax can also be written with the endwhile statement like this


Example

Print $i as long as $i is less than 6:


             $i = 1;
              while ($i < 6):
                echo $i;
                $i++;
              endwhile;



Step 10


If you want the while loop count to 100, but only by each 10, you can increase the counter by 10 instead 1 in each iteration:


Example


           $i = 0;
            while ($i < 100) {
              $i+=10;
              echo $i "<br>";
            }


PHP do while Loop


The do...while loop - Loops through a block of code once, and then repeats the loop as long as the specified condition is true.


Example

Print $i as long as $i is less than 6:


        $i = 1;
          
          do {
            echo $i;
            $i++;
          } while ($i < 6);
          

Note: In a do...while loop the condition is tested AFTER executing the statements within the loop. This means that the do...while loop will execute its statements at least once, even if the condition is false. See example below.


Let us see what happens if we set the $i variable to 8 instead of 1, before execute the same do...while loop again:


Example

Set $i = 8, then print $i as long as $i is less than 6:


        $i = 8;

          do {
            echo $i;
            $i++;
          } while ($i < 6);



The break Statement


With the break statement we can stop the loop even if the condition is still true:


Example

Stop the loop when $i is 3:

         $i = 1;

          do {
            if ($i == 3) break;
            echo $i;
            $i++;
          } while ($i < 6);



The continue Statement


With the continue statement we can stop the current iteration, and continue with the next:



Example

Continue to the next iteration if $i is 3:


         $i = 0;

          do {
            $i++;
            if ($i == 3) continue;
            echo $i;
          } while ($i < 6);



PHP for Loop


The loop - Loops through a block of code a specified number of times.


Syntax

for (expression1, expression2, expression3) {
          // code block
        }

This is how it works:



Example

Print the numbers from 0 to 10:


       for ($x = 0; $x <= 10; $x++) {
          echo "The number is: $x <br>";
        }



The foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.


Example


Loop through the items of an indexed array:


         $colors = array("red", "green", "blue", "yellow");

          foreach ($colors as $x) {
            echo "$x <br>";
          }



Keys and Values


In PHP, arrays play a fundamental role in storing and organizing data. The terms "keys" and "values" are often used in the context of arrays:


Example

Print both the key and the value from the $members array:


         $members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

          foreach ($members as $x => $y) {
            echo "$x : $y <br>";
          }



The foreach Loop on Objects


The foreach loop can also be used to loop through properties of an object:



Example

Print the property names and values of the $myCar object:

       class Car {
          public $color;
          public $model;
          public function __construct($color, $model) {
            $this->color = $color;
            $this->model = $model;
          }
        }
        
        $myCar = new Car("red", "Volvo");
        
        foreach ($myCar as $x => $y) {
          echo "$x: $y <br>";
        }



The break Statement


The break statement can also be used to jump out of a loop.



Example


Stop the loop if $x is "blue":


        $colors = array("red", "green", "blue", "yellow");

          foreach ($colors as $x) {
            if ($x == "blue") break;
            echo "$x <br>";
          }



The continue Statement


With the continue statement we can stop the current iteration, and continue with the next:


         $colors = array("red", "green", "blue", "yellow");

          foreach ($colors as $x) {
            if ($x == "blue") continue;
            echo "$x <br>";
          }



Foreach Byref


The foreach loop can also be used to modify an array's values:


Example


By default, changing an array item will not affect the original array:


        $colors = array("red", "green", "blue", "yellow");

          foreach ($colors as $x) {
            if ($x == "blue") $x = "pink";
          }
          
          var_dump($colors);

BUT, by using the & character in the foreach declaration, the array item is assigned by reference, which results in any changes done to the array item will also be done to the original array:



Alternative Syntax


The foreach loop syntax can also be written with the endforeach statement like this


Example


Loop through the items of an indexed array:


         $colors = array("red", "green", "blue", "yellow");

          foreach ($colors as $x) :
            echo "$x <br>";
          endforeach;



PHP Break


The break statement can be used to jump out of different kind of loops.


Break in For loop


The break statement can be used to jump out of a for loop.


Example

Jump out of the loop when $x is 4:


       for ($x = 0; $x < 10; $x++) {
          if ($x == 4) {
            break;
          }
          echo "The number is: $x <br>";
        }


Break in While Loop


The break statement can be used to jump out of a while loop.


Example

        $x = 0;

          while($x < 10) {
            if ($x == 4) {
              break;
            }
            echo "The number is: $x <br>";
            $x++;
          }


Break in Do While Loop


The break statement can be used to jump out of a do...while loop.


Example

Stop the loop when $i is 3:


        $i = 1;

          do {
            if ($i == 3) break;
            echo $i;
            $i++;
          } while ($i < 6);


Break in For Each Loop


The break statement can be used to jump out of a foreach loop.


Example

Stop the loop if $x is "blue":


         $colors = array("red", "green", "blue", "yellow");

          foreach ($colors as $x) {
            if ($x == "blue") break;
            echo "$x <br>";
          }



PHP Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.


Example

Move to next iteration if $x = 4:


       for ($x = 0; $x < 10; $x++) {
          if ($x == 4) {
            continue;
          }
          echo "The number is: $x <br>";
        }


Continue in While Loop

The continue statement can also be used in a while loop (or any other loop).


Example

Move to next iteration if $x = 4:

         $x = 0;

          while($x < 10) {
            if ($x == 4) {
              continue;
            }
            echo "The number is: $x <br>";
            $x++;
          }


Continue in Do While Loop


The continue statement can also be used in a do...while loop (or any other loop).


Example

Stop, and jump to the next iteration if $i is 3:

        $i = 0;

          do {
            $i++;
            if ($i == 3) continue;
            echo $i;
          } while ($i < 6);


Continue in For Each Loop


The continue statement can also be used in a foreach loop (or any other loop).


Example

Stop, and jump to the next iteration if $x is "blue":


         $colors = array("red", "green", "blue", "yellow");

          foreach ($colors as $x) {
            if ($x == "blue") continue;
            echo "$x <br>";
          }

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java