@elise_daugherty
In PHP, the for
loop is used for executing a set of statements a fixed number of times. The basic syntax for the for
loop is as follows:
1 2 3 |
for (initialization; condition; increment/decrement) { // code to be executed; } |
The three parts of the for
loop are as follows:
Here is an example of using the for
loop in PHP to print numbers from 1 to 10:
1 2 3 |
for ($i = 1; $i <= 10; $i++) { echo $i . "<br>"; } |
In this example, the variable $i
is initialized to 1, and the loop will continue running as long as $i
is less than or equal to 10. After each iteration of the loop, the variable $i
is incremented by 1. The echo
statement is used to output the value of $i
to the screen, followed by a line break using the HTML <br>
tag.
Output:
1 2 3 4 5 6 7 8 9 10 |
1 2 3 4 5 6 7 8 9 10 |
You can modify the for
loop to suit your specific needs by changing the initialization, condition, and increment/decrement values.