for($i=1; $i<=10; $i++){
echo $i."<br>";
}
This will list from number one to ten. 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=1; $i<=10; $i++){
if($i == 5){continue;} // When value=5, below line is skiped.
echo $i."<br>";
}
Above code will print 1 to 10 except the number 5.
for($i=1; $i<=10; $i++){
if($i == 5){continue;} // When value=5, below echo line is skiped.
if($i == 7){break;} // When value=7, the loop is skiped.
echo $i."
";
}
Output is 1,2,3,4 and 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> ";
}
$ar=array("FName"=>"John","Address"=>"Streen No 11 ",
"City"=>"Rain Town","Country"=>"USA");
foreach($ar as $key => $val) {
echo "$key -> $val
";
}
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)
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
vinit | 03-05-2013 |
How to use loop in the form of next if i am click on browser? |