Introduction
Loops help us repeat actions many times without writing the same code again and again.
Loops help us repeat actions many times without writing the same code again and again.
<?php // Example of a for loop for (initialization; condition; increment) { // code to repeat } ?>
The for loop repeats code while the condition is true.
You set where to start, when to stop, and how to move to the next step.
<?php for ($i = 1; $i <= 5; $i++) { echo $i . "\n"; } ?>
<?php $colors = ['red', 'green', 'blue']; foreach ($colors as $color) { echo $color . "\n"; } ?>
This program shows how a loop repeats the print statement three times with different numbers.
<?php // Print numbers from 1 to 3 using a loop for ($i = 1; $i <= 3; $i++) { echo "Number: $i\n"; } ?>
Loops save time and reduce mistakes by avoiding repeated code.
Always make sure your loop has a condition that will eventually stop it.
Loops repeat actions without rewriting code.
They are useful for lists, counting, and repeated tasks.
Make sure loops have a clear stop condition.
for loop in PHP?for ($i = 0; $i < 5; $i++) { to start a for loop.$sum = 0;
for ($i = 1; $i <= 3; $i++) {
$sum += $i;
}
echo $sum;for ($i = 0; $i < 5; $i++)
echo $i
Pass the test and this pattern moves unknown