for loop in php is a block of code with specified numbers of times
php for loop
php for loop is used to repeat a code of block in repeated ways with condition
php for loop Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
All about for loop parameters
- init counter: Initialize the loop counter value
- test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
- increment counter: Increases the loop counter value
Example of for loop in php
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Output
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
other Example of for loop in php
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);