PHP for Loop Structure
What is "for" loop Structure?
Explanation
For structure is a loop structure used in PHP, similar to that in C Language
Syntax
for (initializtion;condition;increment)
{Statements}//true
The "initializtion" is the initial value, "condition" is the condition value,"increment" is the increment or decrement value, every time based on the true value of "condition" the loop is executed, and at the end of every iteration it checks the value of "increment"
Example :
<?php
for($c=0;$c=2;$c++)
{
echo "The value c is $c";
break;
}
?>
In the above example the value of $c is between 0 and 2, after every iteration of the loop the value of $c is increased by 1, then checked with the end values that is 2. If its less than 2, loop continues,else the loop ends
If the "expr2" does not have any value then a break statement can used to terminate the loop. See the example below
Example :
<?php
for ($c = 1; ; $c++) {
if ($c > 5)
{
break;
}
echo $c;
}
?>
Result :
12345