Loops in PHP

Loops or Iterative statements are used to execute a piece of code specific number of times or until a certain condition is met. Consider a case where you have to print some message on the screen 50 times. You will have to write 50 echo statements. But if you use loops, you will have to write the statement only once and the loop will be responsible for printing it multiple times. There are four different types of loops in PHP

  • For loop
  • While Loop
  • Do-While Loop
  • Foreach Loop

For Loop

For loops are used when you already know the exact number of times you want to execute your code. For instance if you want to print some message on the screen 10 times, you can use for loop as follows:

<?php
    for($i =0; $i<10; $i++)
    {
        echo 'Welcome to PHP tutorials<br>';
    }

?>

Download the code
Run the code

A for loop has three parts: initialization, condition and increment. In the above example $=0 is initialization, $<10 is condition and $i++ is increment. Before every iteration it is checked whether the condition returns true or false. If the condition returns true, the loop body is executed followed by the increment statement. This cycle continues until the condition returns false. When the above code is executed, a string will be printed 10 times.

While Loop

While loop is used when you want to keep executing a particular piece of code until a certain condition becomes false. Let’s see how we can print some message on webpage 10 times, using while loop.

<?php
    $k = 0;
    while($k<10) {
        echo 'Welcome to PHP tutorials<br>';
        $k++;
    }
?>

Download the code
Run the code

In while loops, the condition is passed to the while loop as a parameter. The loop keeps on executing until this condition returns false

Do-While

Do-while loop is similar to the while loop but since the do-while loop executes the conditions statement at the end of the loop, the loop executes at least once. In the following example, we shall again print the

<?php
    $k = 0;
    do {
        echo 'Welcome to PHP tutorials<br>';
        $k++;
    }
    while ($k<10);
?>

Download the code
Run the code

In the above code you can see that do-while is similar to while except the condition to execute loop is executed at the end

For Each

The For Each loop is used to iterate over a collection of items. For each loop keeps on executing until all the items in the collection are traversed. In the following example, for each loop is being used to iterate over array. (We shall study arrays in the next section)

<?php 
    $colors = array("red","green","blue","pink","yellow");
    foreach($colors as $color) {
        echo $color.'<br>';
    }
?>

Download the code
Run the code

Thats all about the loops in PHP. However there are two more concepts to remember. In order to break out of the loop before all the executions are completed you can use break statement. Similarly to go back to the start of the loop without executing rest of the code in the loop, you can use continue

<<< Arrays in PHPStrings in PHP >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .