🟡 Control Flow  ·  Lesson 08

Loops in PHP

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.
  • foreach is 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 से दोहराते हैं।
  • foreach arrays पर loop करने का सबसे आसान तरीका।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।