PHP Loops: While, Do While, For, ForEach Loop

Loops in PHP

Like other programming languages, PHP also has Loops. Loops are used to execute a block of code again and again until the specified condition is true. And loops are very helpful for developers to save the effort and time of writing the same code multiple times.

The following two examples have the same output –

<?php
  $names = array('James','Mark','John','Barry','Bruce','Joe');

  echo $names[0].'<br>';
  echo $names[1].'<br>';
  echo $names[2].'<br>';
  echo $names[3].'<br>';
  echo $names[4].'<br>';
  echo $names[5].'<br>';
?>
<?php
  $names = array('James','Mark','John','Barry','Bruce','Joe');

  foreach($names as $single_name){
      echo $single_name.'<br>';
  }
?>

Types of PHP Loops

In PHP, there are four types of loops –

  1. while loop
  2. do-while loop
  3. for Loop
  4. foreach loop

while loop in PHP

The PHP While Loop executes a block of code repeatedly until the specified condition is false.

<?php
  while(condition){
     //block of code
  }
?>

The following example displays the numbers from 1 to 3 –

<?php
  $i = 1;

  while($i < 4){
      echo $i ."<br>";
      $i = $i+1;
  }

  echo "Done!";
?>
Browser Output
1
2
3
Done!

Explanation of the above code

PHP while loop
Explanation of PHP while loop

do-while loop in PHP

The do-while loop is very similar to the while-loop. The only difference between these two loops is –

The while-loop first checks the condition and then executes the block of code, but the do-while loop first executes the block of code then checks the condition. Therefore the do-while executes the block of code at least once because it checks the condition after executes the block of code.

<?php
  do{
    // Block of Code
  }while(condition);
?>

The following example displays numbers from 1 to 5.

<?php
  $i = 1;
  do{
      echo $i.'<br>';
      $i++;
  }while($i <= 5);
?>
Browser Output
1
2
3
4
5

For Loop in PHP

The PHP For Loop is used to execute a block of code for a specified number of times.

<?php
  for(Initialization; Condition; Increment or Decrement){
      // Statement
  }
?>
  • Initialization – Set the loop counter value. Basically, this expression takes a variable which value is always a number (This is the number from where the count begins).
  • Condition – The condition decides how many times the loop will run. Basically, your loop will run continuously until your condition is false.
  • Increment or Decrement – It is used to Increment or Decrement the counter value (the counter value will increase or decrease according to the number of times the loop runs.)

The following example displays the numbers from 1 to 3.

<?php
  for($variable = 1; $variable < 4; $variable++){
      echo $variable.'<br>';
  }
?>
Browser Output
1
2
3

ForEach Loop in PHP

The PHP ForEach loop is a very helpful and easy loop to loop an array or object. This loop is specifically made for arrays and objects, and It won’t work with other data types.

This loop iterates over the array until the array finished.

<?php
  foreach($array_or_object as $value){
      //statement
  }
?>
<?php
  $users = array(
      'John',
      'Mark',
      'Barry',
      'Bruce',
      'Tony'
  );

  foreach($users as $value){
      echo $value.'<br>';
  }
?>
Browser Output
John
Mark
Barry
Bruce
Tony

You can also access the keys of an array or object using this forEach loop.

<?php
  foreach($array_or_object as $key => $value){

      echo $key.'<br>';

  }
?>
<?php
  $data = array(
    "id" => 2,
    "name" => "Ervin Howell",
    "username" => "Antonette",
    "email" => "[email protected]",
    "phone" => "010-692-6593",
    "website" => "w3jar.Com"
  );
      
  foreach($data as $key => $value){
  
    echo "$key => $value <br>";
  
  }
 
 ?>
Browser Output
id => 2
name => Ervin Howell
username => Antonette
email => [email protected]
phone => 010-692-6593
website => w3jar.Com

All the PHP loops execute specific tasks for specific time or condition. But, if you want to skip the specific iteration of a loop or stop a loop at any time, then you can use the Break and Continue statement.

Leave a Reply

Your email address will not be published. Required fields are marked *