PHP Loop

Loop or iteration statements are used to execute the same block of code repeatedly, as long as a particular condition is true.

a. while - loops through a block of code as long as the specified condition is true

Example

<?php
$n=2;
while($n<=20){
   print $n.'<br/>';
   $n=$n+2;
}
?>

Output

2
4
6
8
10
12
14
16
18
20
b. do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
c. foreach - loops through a block of code for each element in an array
d. for - loops through a block of code a specified number of times

Example

<?php
for($num; $num<=10; $num++){
  print $num.'<br/>';
}
?>

Output


1
2
3
4
5
6
7
8
9
10





Next »


Home    |    Learn Programming   |    Learning Management System   |    Computer Based Testing System    |    Contact Us