In PHP, we can use the for loop to execute a block of code for a specific number of times.
The syntax of the for loop is as follows:
for (initialization; condition; increment/decrement) {
// code to be executed
}
Here, initialization is the initial value of a variable that we want to use in the loop. condition is the condition that we want to check before each iteration of the loop. increment/decrement is the amount by which we want to increment or decrement the variable on each iteration. The code inside the loop will be executed repeatedly as long as the condition is true.
Here’s an example that demonstrates how to use a for loop in PHP:
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i . "<br>";
}
?>
In this code, we define a variable $i with a value of 1. We then use a for loop to output the value of $i and increment it by 1 on each iteration. The loop continues to execute as long as $i is less than or equal to 10.
When we run this code, it will output the numbers 1 to 10 on separate lines.