PHP Basic Tutorial
MySQL Connection
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.
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 to true
. 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.Try it yourself
$i = 0
.($i < 5)
is evaluated before each iteration. As long as it is true
, the code inside the loop is executed.$i
is incremented by 1 using $i++
.$i
is no longer less than 5.