In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true.
When the for loop is executed, the variable of the loop is initialized first and then the condition is checked. If the condition is true, then the program control goes to the statement block of the for loop and executes the statements there. When the For Loop statement executes all the statements in the block, it executes the step size part of the loop before exiting the block.
Then does the condition check back. If the condition is true then goes back to the statement block.
This sequence continues as long as the condition of the for loop remains true. Initialization of the loop happens only once.
Syntax:-
for( initialization; condition; increment ){
//statements;
}
Example:-
<?php
echo "This is for loop in action <br>";
for ($index=1; $index < 5; $index++) {
// for(initialization;condition; updation)
echo "This number is $index <br>";
}
echo "For loop has ended";
?>