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.
This is used frequently in many scripts where a common block of code to be executed repeatedly. We can add break statement to stop the loop in between based on matching requirements. We will start with a basic loop and then move to adding different requirements to it.
For and foreach loop in PHP with break statement and creating patterns using nested for loops
Expr3 is executed at the end of the loop. Check the ouput just outside the for 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
break;
By using break we can exit the loop without completing the total required loopings (early exit).
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)
continue
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
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.
break and continue
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.
Step value of increment in loop
So far we have seen each time the variable value is increased by 1. This increase or step value can be changed. Now we will use a step value of 10 so the variable $i will increase its value by 10 ( it was increasing by 1 in previous cases ) in each looping.
for($i=0;$i<=100;$i +=10){
echo $i."<br>";
}
The above code will display 0,10,20 ... till 100
Noter that $i += 10; is same as $i=$i+10;. Similarly $i -=10; is same as $i=$i-10;
Decrement step value in loop
We can reduce the step value in each step of iteration.
for($i=10;$i>0;$i--){
echo $i."<br>";
}
If the condition to check is never meet then it will be an infinite loop execution and PHP execution will hang or stop after reaching maximum execution time.
While handling PHP Arrays foreach function is required to display the elements of an array. Once the foreach function is called the array pointer will reset to the first element of the array. So we need not call the reset() again( Reset() rewinds array's internal pointer to the first element.
). Here is an example of the foreach code.
$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 ";
}
We can get number of elements in an array by using count(). Here we are counting number of elements in array in each iteration. This is not a good idea as the count() function returns the same value in each iteration all the time.