Advertisement
Google Ad Slot: content-top
PHP for Loop
The for loop in PHP is used when you know in advance how many times you need to execute a block of code. It is particularly useful for iterating a known number of times, and it offers more control over loop iterations compared to while and do...while loops.
Syntax
for (initialization; condition; increment/decrement) {
// Code to be executed on each iteration
}
initialization: This expression is executed once at the beginning of the loop. It usually sets a counter variable.condition: The loop continues to run as long as this condition evaluates totrue. It is evaluated before each iteration.increment/decrement: This expression is executed at the end of each iteration. It is usually used to update the loop counter.
Explanation:
- The loop starts with
$i = 0. - The condition
($i < 5)is evaluated before each iteration. As long as it istrue, the code inside the loop is executed. - After each iteration,
$iis incremented by 1 using$i++. - The loop stops when
$iis no longer less than 5.