PHP LOOPS tutorial with example

In this tutorial we learn about Loop in PHP.

Loop means to travel set of code multiple times until the given condition is fulfill.

This means that if we know how many times we need this statement to execute.

In PHP, User wants to execute the statement number of times until and unless the given condition reach they use loop.

It will save time and effort of programmer and make task more easier and faster.

PHP supports different types of loop. Here we have list of various loops.

1. For Loop

2. While Loop

3. Do..While Loop

4. Foreach Loop

Lets take a deep information about all the loops one by one.

FOR Loop

For loop used when you know how many times the statement of code will run.

It means that the programmer will know how many time the iteration need to execute.

For Loop also known as “Entry Control Loop“.

Means that the number of iteration is already defined before entering the loop.

There is three parameters in the declaration of for loop.

Syntax

for(initialization; condition; increment)
{
    // Code to be executed
} 

Initialization

– First initialize the loop variable to some counter value.

– For the first time this will run unconditionally before entering the body of loop.

Condition

– It evaluates at the beginning of every iteration.

– If the condition evaluates true the body of loop will execute.

– If the condition evaluates false the body of loop will not executed it terminate the loop.

– If the condition evaluates false the body of loop will not executed it terminate the loop.

Increment

– After the loop execution this increment or decrement operator will increase or decrease the variable value.

– It evaluate at the end of every iteration.

EXAMPLE

<!DOCTYPE html>
<html>
<body>
<?php  
for ($i = 0; $i <= 10; $i++) {
  echo "The value is: &nbsp&nbsp&nbsp&nbsp $i <br>";
}
?>  
</body>
</html>

OUTPUT

The value is:      0
The value is:      1
The value is:      2
The value is:      3
The value is:      4
The value is:      5
The value is:      6
The value is:      7
The value is:      8
The value is:      9
The value is:      10 

While Loop

While loop is also “Entry control loop” as similar as For Loop.

The value of condition checked every time before entering the loop.

In this while loop first check the condition defined into while loop if the condition is true then only code of block into while loop portion will execute.

Every time first check the condition after that run the code if block.

If the condition return false the execution of code into while loop is terminated.

We can use while loop to read the records from database and display on front-end.

Syntax

while(check condition)
{
   code to be executed
}

EXAMPLE

<!DOCTYPE html>
<html>
<body>
<?php  
$text = 'a';
 
while($text <= 'g') 
{
  echo "The alphabet is: &nbsp&nbsp&nbsp $text <br>";
  $text++;
} 
?>  
</body>
</html>

OUTPUT

The alphabet is:     a
The alphabet is:     b
The alphabet is:     c
The alphabet is:     d
The alphabet is:     e
The alphabet is:     f
The alphabet is:     g 

Do…While Loop

Do…while loop is also known as “Exit control loop“.

This loop is very similar to while loop the only difference between while and do while is Do.

On this loop first perform the expression then it check the condition into while loop.

If the condition is true then it will perform number if iteration for code of block until the specified condition is false.

In short the do while loop is execute atleast once during the execution after that checks the condition.

Then it will repeat the loop while given condition is true.

Syntax

do
{
  code to be executed
}
while (condition)

EXAMPLE

<!DOCTYPE html>
<html>
<body>
<?php 
$text = 'A';
do {
  echo "The alphabet is:&nbsp&nbsp $text <br>";
  $text++;
} while ($text <= 'E');
?>
</body>
</html>

OUTPUT

The alphabet is:   A
The alphabet is:   B
The alphabet is:   C
The alphabet is:   D
The alphabet is:   E 

Foreach Loop

PHP 4 introduces foreach loop.

Foreach loop works only for array value.

It is used to iterate for every pair of key/value in array.

For every iteration of foreach loops, the current array element assign to $value and then the array pointer is shifted by one until it reaches the last array pointer.

Syntax

foreach ($array as $value)
{
  code to be executed;
} 

EXAMPLE

<!DOCTYPE html>
<html>
   <body>
   
      <?php
        $mark = array("Cherry"=>"95", "Smith"=>"97", "Alen"=>"93");
        foreach($mark as $a => $value) 
        {
        echo "$a = $value<br>";
        }
      ?>
      
   </body>
</html>
Cherry = 95
Smith = 97
Alen = 93