Loops

1. For

For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations are known in advance.

Syntax

for(Initialization; Condition; Increment/decrement){  
//code  
} 

Example


# print 1 to 10 numbers in php using for loop
<?php
  for ($i = 1; $i <= 10; $i++) {
    echo("$i\n");
  }
?>

Check Result here

2. Foreach

Foreach loop is used to loop through each element or key/value pair in an array. It works only on arrays.

Syntax

foreach ($array as $value) {
  //code 
}

Example

<?php
$directions = array("East", "West", "North", "South");

foreach ($directions as $value) {
  echo "$value \n";
}
?>

Check Result here

3. While

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

Syntax

while(condition){  
//code 
}  

Example

# print 1 to 10 numbers in php using while loop
<?php
   $i=1;
   while ( $i <= 10) {
     echo("$i\n");
     $i++;
   }
?>

Check result here

4. Do-while

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

Syntax

do{  
//code 
}while(condition); 

Example

// print 1 to 10 numbers in php using do-while loop
<?php
  $i=1;
  do {
    echo("$i\n");
    $i++;
  } while ($i<=10);
?>

Check result here