Loop in PHP
Subject: Web development using PHP and MySQL
Loop or iteration statements are used to execute the same block of code repeatedly, as long as a particular condition is true.
while loop - loops through a block of code as long as the specified condition is true
Example
$n=2;
while($n<=20){
print $n.'
';
$n=$n+2;
}
?>
Output
2
4
6
8
10
12
14
16
18
20
do while loop- loops through a block of code once, and then repeats the loop as long as the specified condition is true.
foreach loop - loops through a block of code for each element in an array.
for loop - loops through a block of code a specified number of times, with three arguments (initial; terminate condition; increase or decrease)
Example
for($num; $num<=10; $num++){
print $num.'
';
}
?>
By:
Benjamin Onuorah
Login to comment or ask question on this topic
Previous Topic Next Topic