🟡 Control Flow · Lesson 08
Loops in PHP
PHP Loops
PHP has
for, while, do-while and foreach (special for arrays).for and while
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
// 1 2 3 4 5
$n = 1;
while ($n <= 3) { echo $n; $n++; }
// 123
?>
foreach (for arrays)
<?php
$fruits = ["apple", "mango", "kiwi"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>apple
mango
kiwi
Summary
- for/while/do-while repeat by condition.
foreachis the easiest way to loop over arrays.
PHP Loops
PHP में
for, while, do-while और foreach (arrays के लिए खास) हैं।for और while
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
// 1 2 3 4 5
$n = 1;
while ($n <= 3) { echo $n; $n++; }
// 123
?>
foreach (arrays के लिए)
<?php
$fruits = ["apple", "mango", "kiwi"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>apple
mango
kiwi
सारांश
- for/while/do-while condition से दोहराते हैं।
foreacharrays पर loop करने का सबसे आसान तरीका।
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.