PHP Tutorial


For Loop, While Loop, ForEach, Do While - PHP 7 Loops


PHP Loop

PHP loops are used when one more statement has to be executed more than once. by using loops, the program will keep repeating until the given condition is fulfilled.

The PHP loop is used to execute the same group of code of the program until a particular condition is met.

The use of PHP loops does not require the developer to write the same code over and over again. This saves coding time.

PHP provides four types of loops:

  • PHP for loop
  • PHP while loop
  • PHP do...while loop
  • PHP for each loop

PHP for Loop

PHP for() loop is also known as "Entry Control Loop". which is used when the developer knows in advance how often the group of code needs to be executed.

Note: The PHP for() loop accepts mainly three parameters, namely the initialization, the test condition and the increment/decrement (update).

Syntax:

    for (initialization; test condition; increment or decrement) {

           // Group of code to be executed.

        }

There are three main parameters to the code. 

  1. Initialization: initialize a variable with some value. This variable acts as the loop counter.
  2. Test condition: The condition to be checked is defined after each iteration/cycle of the loop. If the condition returns true, a loop is executed.
  3. Increment or Decrement (Update): increment or decrement (Update) the loop counter as per the requirements.

Example:

The following example of PHP for loop.

    <!DOCTYPE html>
    <html>
    <body>

    <?php  

    for ( $j = 0; $j <= 5; $j++ ) {
          echo "The value is: $j <br>";
    }

    ?>  

    </body>
    </html>

Explain Example:

$j = 0;  // Start the loop counter ($j), and set the start value to 0.

$j <= 5; // The loop continues until the value of $j is less than or equal to 5(update value).

$j++  // Increase the loop counter value by 1 for each iteration.

PHP while Loop

PHP while loop first checks the condition and if the condition is met true, then it enters the loop and executes the block of code and the process continues as long as the condition true.

Note: While PHP works for() loop loops but there is a slight difference. The PHP while loop checks the status first and then executes a group of code. And initializes the first variable for() loop then checks the given condition. 

Syntax: 

    while (test condition) 
        {

            // Group of code to be executed.

        }

Example:

The following example of PHP while loop.

    <!DOCTYPE html>
    <html>
    <body>

    <?php 

    $k = 1; 

    while ( $k <= 10 ) { 
        $k++; 
        echo "The value is: $k <br>"; 
    } 

    ?> 

    </body>
    </html>

Explain Example:

$k = 1;  // Start the loop counter ($k), and set the start value to 0.

$k <= 10; // The loop continues until the value of $k is less than or equal to 10 (update value).

$k++  // Increase the loop counter value by 1 for each iteration.


PHP do...while Loop 

The PHP do...while loop is an exit control loop that first enters the loop, then executes a group of code and finally, the condition is checked.

When using the PHP do...while loop, a group of code is executed at least once and once a group of code is executed, the program is executed as long as the condition is true.

Syntax: 

    do {

            //Group of code is executed

    } while (test condition);

Example:

The following example of PHP do...while loop.

    <!DOCTYPE html>
    <html>
    <body>

    <?php 

    $l = 5;
    do {
        echo " The value is: $l <br>";
        $l++; 

    } while ($l < 25); 

    ?> 

    </body>
    </html>

Explain Example:

first sets a variable $l to 1 ($l = 5). Then, the PHP do...while loop will execute some output, and then update the variable $l with 1 ($l++). Then the condition is checked (is $l less than 25) and the loop will continue to run as long as $l is less than 25

PHP foreach Loop

The PHP foreach loop is used to iterate the array element. 

In the PHP for each loop, the value of the array element is assigned to the $ value. In the for each loop, first, an array is declared, then a looping process is done through the array elements, then the array elements are executed.

Syntax:

foreach( $array as $value ) {
    // group of code to be executed
}

or

foreach( $array as $key => $value) {
    // group of code to be executed
}

Example:

The following example of PHP for each loop.

Example 1:

<!DOCTYPE html>
<html>
<body>

<?php 

$language = array("PHP", "PYTHON", "NODE JS", "RUBY"); 

foreach ($language as $value) { 
    echo "$value <br>"; 
} 

?> 

</body>
</html>

Example 2:

<!DOCTYPE html>
<html>
<body>

<?php
$height = array("John"=>"165cm", "James"=>"183cm", "Maxx"=>"190cm");

foreach($height as $key => $value) {
  echo "$key = $value<br>";
}
?>

</body>
</html>