PHP Loops

Last Updated : May 22, 2021

PHP Loops are used to execute specific code blocks until a certain condition is satisfied. These loops help avoid writing more code for a repetitive operation like printing values from the array, printing information from database tables, etc. Please find below the list of loops available in PHP.

We will learn about them in detail below with examples.

PHP for Loop

PHP for loop used to execute a code block with certain times until specific conditions are met. This for loop is one of most used one by programmers because of this simplicity and convenience.

Syntax

for (initialization, condition; increment){
  //code block to execute until conditions met
}

Please find below the examples,

<?php
for($i=1; $i<=10; $i++) {
  echo "Hello World $i <br>";
}
echo "<br>Increment inside loop. <br>";
for($i=1; $i<=10;) {
  echo "Hello World $i <br>";
  $i++;
}
echo "<br> Display array values<br>";
$rainbow = array('Red', 'Orange', 'Yellow', 'Green',
                 'Blue', 'Indigo', 'Violet');
for($i=0; $i<=7; $i++) {
  echo $rainbow[$i] .'<br>';
}
?>

PHP foreach Loop

PHP foreach loop used to iterate the arrays effectively; we can get each key-value pair from the array. We can use for loop to get values from the array indirectly; this forearch loop works better with arrays.

Syntax

foreach (array as $value) {
  //code block to execute
}
foreach (array as $key => $value) {
  //code block to execute
}

Please find below the examples,

<?php
// array example
$rainbow = array('Red', 'Orange', 'Yellow', 'Green',
                 'Blue', 'Indigo', 'Violet');
foreach($rainbow as $key=>$value) {
  echo $key .'='. $value .'<br>';
}
echo "<br><br>";
// without key
foreach($rainbow as $value) {
  echo $value .'<br>';
}
?>

PHP while Loop

PHP while loop used to execute a code block until the conditions are satisfied. This while loop checks the conditions first and then executes a code block if it’s true otherwise it will skip the code block.

Syntax

while (conditions) {
  //code block to execute
}

Please find below the examples,

<?php
// while loop
$i = 1;
while($i <= 5) {
  echo "I am number $i <br>";
  $i++; //increment
}
echo "<br>";
//example-2
$j = 10;
while($j <= 100) {
  echo "I am number $j <br>";
  $j += 10; //increment
}
//example-3
$k = 200;
while($k <= 100) {
  echo "I am number $i <br>";
}
?>

PHP do...while Loop

PHP do...while loop is the same as a while loop but do...while will execute a code block at least one time regardless of the given conditions. This is the main difference between the while and do..while loop and this loop also will be executed until the conditions are satisfied.

Syntax

do {
  //code block to execute
} while (conditions);

Please find below the example,

<?php
// do...while loop
$i = 1;
do {
  echo "I am number $i <br>";
  $i++; //increment
} while($i <= 5);
echo "<br>";

//example-2
$i = 100;
do {
  echo "I am number $i <br>";
} while($i <= 5);
?>

PHP break Statement

The PHP break statement is used to break a current execution from the loop without going back to check the conditions. This break statement works and breaks an execution with for, foreach, while, do...while, and switch structures. Please find below the examples,

<?php
echo "for loop example <br>";
for($i=1; $i<=5; $i++) {
  echo "I am number $i <br>";
  if($i == 3) break;
}

echo "<br>foreach loop example <br>";
$numbers = array("one", "two", "three", "four", "five");
foreach($numbers as $key=>$value) {
  echo $value ."<br>";
  if($value == "four") break;
}

echo "<br>while loop example <br>";
$i = 1;
while($i <= 1000) {
  echo "I am number $i <br>";
  if($i > 6) break;
  $i++;
}

echo "<br>do...while loop example <br>";
$i = 1;
do {
  echo "I am number $i <br>";
  if($i > 6) break;
  $i++;
} while($i <= 1000);

echo "<br>switch example <br>";
switch(10) {
  case(5):
    echo "This is Five";
    break;
  case(10):
   echo "This is Ten";
   break;
}
?>

PHP continue Statement

PHP continue statement used to return the execution pointer to the beginning of the loop. The pointer will check the conditions and execute the code block as usual again. This continue statement works better when we want to skip the large part code and start the loop from the beginning. Please find below the examples,

<?php
//while loop example
$i=1;
while($i < 10) {
  $i++;
  if ($i % 2 == 0) continue;
  echo "I am number $i <br>";
}
?>

In this tutorial we learned about PHP Loops and its control statements with their example usages. We will use these loops in our advanced PHP tutorial chapter very effectively. Let’s move into the next chapter.