Loops in PHP

Admin   PHP   193  2020-10-05 09:01:19

Loops in PHP are used to execute the same block of code a specified number of times. PHP supports the following 4 types of loops:

  • For Loop - Loops through a block of code a specified number of times.
  • While Loop - Loops through a block of code if and for as long as a condition has been specified true.
  • Do ... while loop - Loops through a block of code once, and then repeats that loop as long as a given condition becomes true.
  • Foreach loop - Loops through a block of code for each element in an array.

At the end of the chapter, we will discuss the continue and break keywords that control the execution of loops in PHP.

PHP for loop

The for statement is used when you know in advance how many times you want to execute a command or block in PHP

PHP for loop

Syntax

for (initialize; condition; value_ increment)
{
   code to execute
}

Initializer is used to set the start value for the iteration counter variable. A variable can be declared here for this purpose and its traditional name is $i.

For example

The following example creates 5 iterations and changes the assigned value of two variables through each iteration.

<html>
   <body>
      
      <?php
         $a = 0;
         $b = 0;
         
         for ($i = 0; $i <5; $i++)
         {
            $a + = 10;
            $b + = 5;
         }
         
         echo ("After the loop, values ​​a = $a and b = $b");
      ?>
   
   </body>
</html>

While loop in PHP

The while statement in PHP will execute a block of code if and for as long as a condition is specified as true.

If the condition is set to true, then the block of code will be executed. After the code is executed, the conditional expression is evaluated again, and the loop continues to execute until the conditional expression becomes false.

While loop in PHP

Syntax

while (condition)
{
   the part of code to be executed if the condition becomes true
}

For example

The following example decrements a variable through each iteration and increments the counter until it reaches 10, then evaluates to false and the loop ends.

<html>
   <body>
   
      <?php
         $i = 0;
         $num = 30;
         
         while ($i <10)
         {
            $num--;
            $i ++;
         }
         
         echo ("The loop stops at the values ​​i = $i and num = $num");
      ?>
      
   </body>
</html>

PHP do… while loop command

The do… while command will execute a block of code at least once. It will then repeat the loop as long as the condition is true.

Syntax

do
{
   code to execute
}
while(condition);

For example

The following example will increment the value of i at least once, and then it will continue to increase the variable i as long as it is less than 10.

<html>
   <body>
   
      <?php
         $i = 0;
         
         do
            $i++;
         }while ($i <10);

         echo ("The loop stops at value i = $i");
      ?>
      
   </body>
</html>

PHP foreach loop command

The foreach loop statement is used to loop through an array. For each iteration, the value for the current array element is assigned to $value and the array pointer is moved to the next element, and in the next iteration the next element is processed.
Syntax
 

foreach (Array as value)
{
   code to execute

}

For example

Try the following example to list the values ​​of an array.

<html>
   <body>
   
      <?php
         $array = array(1, 2, 3, 4, 5);
         
         foreach ($array as $value)
         {
            echo "The array element value is $value <br />";
         }
      ?>
      
   </body>
</html>

PHP break statement

The break keyword is used to abruptly terminate the execution of a loop.

PHP break statement

The break is placed inside the block. It gives you full control whenever you want to exit the loop. After exiting the loop, the command immediately following the loop will be executed.

<html>
   <body>
   
      <?php
         $i = 0;
         
         while ($i <10)
         {
            $i++;
            if ($i == 3) break;
         }
         echo ("The loop stops at value i = $i");
      ?>
   
   </body>
</html>

PHP continue statement

The continue keyword in PHP is used to stop the current loop but not to end it.

Like the break statement , the continue statement is placed inside the block containing the code that the loop executes, preceded by an expression that checks the condition. Upon encountering the continue statement, the rest of the loop is skipped and continued for the next loop.

PHP continue statement

For example

The following loop prints the value of an array, if the condition is met, it prints, otherwise, it skips and continues the loop.

<html>
   <body>
   
      <?php
         $array = array(1, 2, 3, 4, 5);
         
         foreach ($array as $value)
         {
            if ($value == 3) continue;
            echo "The value of the section is $value <br />";
         }
      ?>
   
   </body>
</html>