The for loop of PHP is similar to that of the C language. It has three parameters. first parameter is used to initialize the variable value, second parameter is used to define the condition and third parameter is used to increase or decrease the variable value.
Syntax:-
for (start_expression; condition_expression; increment_or_decrement_expression)
Example1:- Suppose, You have a variable $x and that has value 1 and you increase the $x value until $x value will be 4.
<?php
for ($x = 1; $x < 5; $x++) {
print $x ."<br/>";
}
?>
1
2
3
4
2
3
4
Example2:- Suppose, You have a variable $x and that has a value 5 and you decrease the $x value until $x value will be 2
<?php
for ($x = 5; $x>1; $x--) {
print $x ."<br/>";
}
?>
5
4
3
2
4
3
2