PHP Loops

loops in php

PHP Loops are used to execute the same block of code a specified number of times. PHP supports following four loop types.

  1. for − loops through a block of code a specified number of times.
  2. while − loops through a block of code if and as long as a specified condition is true.
  3. do…while − loops through a block of code once, and then repeats the loop as long as a special condition is true.
  4. foreach − loops through a block of code for each element in an array.

We will discuss about continue and break keywords used to control the loops execution.


for loop

PHP for loop can be used to traverse set of code for the specified number of times. It should be used if the number of iterations is known otherwise use while loop. This means for loop is used when you already know how many times you want to execute a block of code.

Flow Chart :

Example

The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −

<html>

   <body>

      

      <?php

         $a = 0;

         $b = 0;

         

         for( $i = 0; $i<5; $i++ ) {

            $a += 10;

            $b += 5;

         }

         

         echo ("At the end of the loop a = $a and b = $b" );

      ?>

   

   </body>

</html>

The while loop statement

PHP while loop can be used to traverse set of code like for loop. The while loop executes a block of code repeatedly until the condition is FALSE. Once the condition gets FALSE, it exits from the body of loop.

It should be used if the number of iterations is not known.

Flow Chart :

Example

<html>

   <body>

   

      <?php

         $i = 0;

         $num = 50;

         

         while( $i < 10) {

            $num--;

            $i++;

         }

         

         echo ("Loop stopped at i = $i and num = $num" );

      ?>

      

   </body>

</html>

The do…while loop statement

PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while loop is guaranteed to run at least once.

Example

<html>

   <body>

   

      <?php

         $i = 0;

         $num = 0;

         

         do {

            $i++;

         }

         

         while( $i < 10 );

         echo ("Loop stopped at i = $i" );

      ?>

      

   </body>

</html>

The foreach loop statement

The foreach loop is used to traverse the array elements. It works only on array and object. It will issue an error if you try to use it with the variables of different datatype.

The foreach loop works on elements basis rather than index. It provides an easiest way to iterate the elements of an array.

Flow Chart :

Example

<html>

   <body>

   

      <?php

         $array = array( 1, 2, 3, 4, 5);

         

         foreach( $array as $value ) {

            echo "Value is $value <br />";

         }

      ?>

      

   </body>

</html>

The break statement

PHP break statement breaks the execution of the current for, while, do-while, switch, and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only.

The break keyword immediately ends the execution of the loop or switch structure. It breaks the current flow of the program at the specified condition and program control resumes at the next statements outside the loop.

Flow Chart :

Example

<html>

   <body>

   

      <?php

         $i = 0;

         

         while( $i < 10) {

            $i++;

            if( $i == 3 )<b>break;</b>

         }

         echo ("Loop stopped at i = $i" );

      ?>

   

   </body>

</html>

The continue statement

The PHP continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition.

The continue statement is used within looping and switch control structure when you immediately jump to the next iteration.

Flow Chart :

Example

<html>

   <body>

   

      <?php

         $array = array( 1, 2, 3, 4, 5);

         

         foreach( $array as $value ) {

            if( $value == 3 )<b>continue;</b>

            echo "Value is $value <br />";

         }

      ?>

   

   </body>

</html>

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *