For, While, and Foreach
PHP supports for, while, do-while, and the powerful foreach loop for arrays.
Example
<?php
// For loop
for ($i = 0; $i < 5; $i++) {
echo "$i ";
}
// While loop
$count = 0;
while ($count < 5) {
echo "$count ";
$count++;
}
// Foreach — iterate over arrays
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo "$fruit ";
}
// Foreach with key => value
$ages = ["Alice" => 25, "Bob" => 30];
foreach ($ages as $name => $age) {
echo "$name is $age years old\n";
}
// Break and continue
for ($i = 0; $i < 10; $i++) {
if ($i === 5) break;
if ($i % 2 === 0) continue;
echo "$i "; // 1 3
}
?> 