What is Looping Structures?

A Looping structure allows you to execute block of code repetitively to the specified number of times until condition gets true. It is basic in every programming languages.

for loop

For loop basically execute block of code at specified number of times.

Syntax

<?php

    for (initialization; condition; increment/decrement)

    {

/* Code to be executed; */

    }

?>

Here,

  • "for… (…)" is looping block.
  • "initialization" basically an integer. It is used to set the counter’s initial value. 
  • "condition" is evaluated for each one of php execution. In this if the condition return false then the loop terminate otherwise it continue looping. 
  • "increment/decrement" is code to be executed if the condition is false.

Example

<?php

    for ($num = 0; $num <= 5; $num++)

    {

        echo "Number is: " . $num . "<br/>";

    }

?>

Output

Number is: 0

Number is: 1

Number is: 2

Number is: 3

Number is: 4

Number is: 5

foreach loop

The php for each loop is used to get values from array. Following the basic syntax of for each loop. 

Syntax

<?php

    foreach($arrayvariable as $arrayvalue)

    {

/* Code to be executed; */

    }

?>

Here,

  • "foreach(…){…}" is php loop block code.
  • "$arrayvariable" is variable to be looped.
  • "$arrayvalue" is temporary variable which store current array item values.
  • "code to be executed" is block of code which operates array values. 

Example

<?php

    $month = array(

        "January",

        "February",

        "March",

        "April",

        "May",

        "June"

    );

    

    foreach ($month as $mvalue)

    {

        echo "Month is: " . $mvalue . "<br/>";

    }

?>

Output

Month is: January

Month is: February

Month is: March

Month is: April

Month is: May

Month is: June

while loop

Basically while loop is used to execute block of code repeatedly until the set condition get false. You can use while loop to get records from database.

Types of while loops

1. while.. : It is check condition first. If condition is true code of block executed until the condition is true and if condition is false then execution of loop is terminated.

2. do…while : It is execute code of block at least one time either condition is true or false.

while loop

Syntax

<?php

while( condition )

{

/* Code to be executed; */

}

?>  

Here, 

  • "while(…) {…}" is loop block code.
  • "condition" is condition to be check by the while loop.
  • "code to be executed" is code to be executed if your condition is true.

Example

<?php

    $num = 0;

    while ($num <= 5)

    {

        echo "Your number is: " . $num . "<br/>";

        $num = $num + 1;

    }

?>

Output

Your number is: 0

Your number is: 1

Your number is: 2

Your number is: 3

Your number is: 4

Your number is: 5

do…while loop

Syntax

<?php

do

{

/* Code to be executed; */

}while( condition );

?>

Here,

  • "do{…}while(…)" is loop block code.
  • "condition" is condition to be check by the do… while loop
  • "code to be executed" is code that executed at least one time by do… while loop. 

Example

<?php

    $num = 5;

    do

    {

        echo "Your number is: " . $num . "<br/>";

    } while ($num < 5);

?>

Output

Your number is: 5

Note: In above example output is “Your number is: 5” because condition is false so at least one time your block of code executed then it will terminate your loop.