PHP for loop

general syntax of the PHP for loop :

for (exprEvaluateAtBeginning ; exprEvaluateConditionToFinishTheLoop ; exprevaluateAfterEachIteration)
{
expr;
}

exprEvaluateAtBeginning : this expression is evaluate only one time at the beginning of the loop
exprEvaluateConditionToFinishTheLoop is evaluate at each iteration. If true the loop goes on, if false the loop ends. It needs to be a boolean expression and sure to become false for be sure to finish the loop, otherwise you will have an infinite loop.
exprevaluateAfterEachIteration is evaluated after each iteration.

 

Each expression of the PHP for loop can contains 0 , 1 or many statements separate by a comma « , ».

Classic use :

you use the for loop by running a counter and increment it until you reach a certain limit.

 

$limit = 10;
for ($i=0; $i < $limit; $i++)
{
echo $i. »<br /> »;

}

result : you display the number :
0
1
2
3
4
5
6
7
9

Leave a Comment