articles

Home / DeveloperSection / Articles / PHP Loops

PHP Loops

Anonymous User6455 08-Sep-2011

Before discussing about PHP Loops let’s should know what is loop? “Loops execute a block of code for specified number of times, or while a specified condition is true”. Often when you write code, you want to execute some line of code over and over again, to perform this task, use loops.

In PHP, we have the following looping statement;
  1.        while loop
  2.        Do…while  loop
  3.        for loop
  4.        foreach loop

Now let’s we have a brief idea about all (while, do…while, for, foreach).

while loop:

while loop execute block of code at certain condition.

Syntax:

while (condition)

 {

    codetobeexecutedwhenconditionistrue;

 }

Let’s we have an example, how to implement while loop in PHP.

Example:

<html>
   <head>  <title>While Loop Test </title>
       </head>
 <body>
    <?php
              // Declare variables
                     $flag = 10;
                     // start while loop
                     while($flag > 0)
                     {
                       echo "Flag value is : ". $flag."</br>";
                       $flag--;
                     }
       ?>
 </body>
</html>
Output:

PHP Loops

do…while loop:

The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.

Let’s we have an example, how to implement do…while statement or loop in PHP.

Syntax:
 do
    {
      code to be executed ;
    }
while (condition);
Example:
<html>
<head>
  <title> do while test</title>
</head>
<body>
       <?php
              // Declare variables
              $flag = 25;
             
              do
              {
                     print "flag value is : ". $flag. "</br>";
                     $flag--;
              } while($flag > 0);
       ?>
       </body>
</html>
Output:

PHP Loops

‘for’ loop:

for loop is used when you know in advance how many times the code should be execute.

Let’s we have an example, how to implement for loop in PHP.

Syntax:
for(init ; condition ; increment)
 {
    code to be executed;
 }
 
Example:
<html>
    <head>
       <title>For loop test </title>
    </head>
<body>
 <?php
       // Declare two-dimensional array
       $matrix  = array (        array (1,2,3),
                      array (4,5,6),
                      array (7,8,9)
                          );
 
       echo "<h3>"."This is two dimensional array"."</h3>"."</br>";
 
       // Print two dimensional array element
 
       for($row = 0 ; $row <3 ; $row++)
        {
           for($col= 0 ;$col <3 ;  $col++)
              {
                 echo $matrix[$row][$col]."\t"."\t";
               }
           echo "</br>";
       }
       ?>
</body>
 
</html>
Output:

PHP Loops

foreach loop:

The foreach loop is used to loop through arrays. For every loop iteration, the value of the current array element is assigned to temporary variable (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.

Syntax:
 foreach($array_name as $temp_variable)
 {
     code to be executed ;
 }

 

Let’s we have an example, hoe to implement foreach loop in PHP;

Example:
<html>
    <head>
       <title>Foreach loop test </title>
    </head>
<body>
 <?php
              // Declare  one- dimensional array
              echo "<h3>"."This is one- dimensional array"."</h3>"."</br>";
              $subject = array(10,9,8,2,3,4,1,6,78,52,42,39);
             
              // print one dimensional element using foreach loop
              foreach ($subject as $value)
               {
                     echo ( "Array element is : ".$value."</br>");
              }
       ?>
</body>
 
</html>
Output:

PHP Loops



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By