---
title: "PHP Loops"  
description: "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 c"  
author: "Anonymous User"  
published: 2011-09-08  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/722/php-loops  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# PHP Loops

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**)**

## {

code to be executed when condition is true**;**

## }

##### 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](https://www.mindstick.com/mindstickarticle/30ee5091-5a71-41d5-abea-e581374805b9/images/8a8b5e1c-0491-4924-8b45-c0831ca1d05f.png)

##### 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](https://www.mindstick.com/mindstickarticle/30ee5091-5a71-41d5-abea-e581374805b9/images/a83f78a4-c030-4b78-a0d9-fef07b3ccda2.png)

##### ‘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](https://www.mindstick.com/mindstickarticle/30ee5091-5a71-41d5-abea-e581374805b9/images/d88197b7-b3da-40be-8020-b524bbed02e3.png)

##### 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](https://www.mindstick.com/mindstickarticle/30ee5091-5a71-41d5-abea-e581374805b9/images/c96906f5-c4a3-456a-9aff-5053f9602430.png)

\

---

Original Source: https://www.mindstick.com/articles/722/php-loops

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
