PHP - For Loop

Loops in PHP are used to perform a task repeatedly.

PHP for loop can be used to traverse set of code for the specified number of times. This means for loop is used when you already know how many times you want to execute a block of code.

Syntax

for(initialization; condition; increment/decrement){  

//code to be executed  

}  

Parameters

initialization - Initialize the loop counter value. The initial value of the for loop is done only once. This parameter is optional.

condition - Evaluate each iteration value. The loop continuously executes until the condition is false. If TRUE, the loop execution continues, otherwise the execution of the loop ends.

Increment/decrement - It increments or decrements the value of the variable.

Example

<?php    

for($n=1;$n<=10;$n++){    

echo "$n<br/>";    

}    

?>