The for loop in PHP is used when the number of iterations is known in advance. It is commonly used for counting, repeating tasks, and numerical operations.
// Basic PHP for loop syntax
for (initialization; condition; increment) {
// code to be executed
}
// Loop from 1 to 5 and print values
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i<br>";
}
?>
The loop starts at $i = 1, checks the condition $i <= 5, and prints each number until the condition becomes false.
Adjust the parameters below to see how the PHP for loop logic changes and what the resulting output would look like.
for loops when iteration count is fixedforeach for array readabilityfor_loop.php