for($i=1; $i<=10; $i++){
echo $i."<br>";
}
This will list from number one to ten. In above case the loop is executed 10 times and each time the value of $i is increased by 1. So in the first loop the value of $i became 1 and in the last loop the value equals to 10. Next time when the value of $i becomes 11 the condition checking fails and the loop is escaped without executing. Here condition is checked ( Expr 2) at the staring of the loop.
for($i=1; $i<=5; $i++){
echo $i."<br>";
}
echo " Value of \$i Outside the loop: ".$i;
Output is here
1
2
3
4
5
Value of $i Outside the loop: 6
for($i=1; $i<=10; $i++){
if($i > 5){break;}
echo $i."<br>";
}
This will list from 1 to 5 only. The If condition inside the for loop will exit the loop when the value of $i exceeds 5 ( that is when it equals to 6)
for($i=0;$i<=100;$i +=10){
echo $i."<br>";
}
The above code will display 0,10,20 ... till 100$i += 10;
is same as $i=$i+10;
. $i -=10;
is same as $i=$i-10;
for($i=10;$i>0;$i--){
echo $i."<br>";
}
$a = array (10, 12, 13, 25);
foreach ($a as $i) {
print "Present value of \$a: $i <br> ";
}
This will list all the values of the array from starting to end. You can read more on arrays$a = array (10, 12, 13, 25);
for($i=0;$i<count($a);$i++) {
print " $a[$i]<br> ";
}
We can get the number of elements and use that in our condition checking.
$a = array (10, 12, 13, 25);
$no=count($a); // Number of elements in array
for($i=0;$i<$no;$i++) {
print " $a[$i]<br> ";
}
for($i=1; $i<=10; $i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br>";}
for($v1=1;$v1<=10;$v1++){
for($i=1;$i<11;$i++){
echo "$v1 x $i = ".$v1*$i;
echo ', ';
}
echo "<br>";
}
How nested for loops are used to create patterns (demo)
vinit | 03-05-2013 |
How to use loop in the form of next if i am click on browser? |