Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 08

Loops in PHP (for, while, do-while, foreach)

In this lesson: Learn all PHP loops with syntax, examples and output — print tables, loop arrays with foreach, break and continue. Hindi & English.

What loops are for

A loop repeats a block of code many times, so you do not have to write it again and again. Want to print 1 to 100, or go through every item in a list? A loop does it in a few lines. PHP has four loops: for, while, do-while and foreach.

💡 Every loop needs a way to stop, or it runs forever. Usually a counter that changes each round takes care of that.

The for loop

Use for when you know how many times to repeat. It has three parts: start, condition, and update.

<?php
  for ($i = 1; $i <= 5; $i++) {
      echo $i . " ";
  }
?>
1 2 3 4 5

It reads as: start at $i = 1, keep going while $i <= 5, and add 1 each round ($i++). When $i becomes 6 the condition is false and the loop stops.

while and do-while

while checks the condition first; do-while runs once then checks.

<?php
  $i = 1;
  while ($i <= 3) {
      echo $i . " ";
      $i++;
  }
  // do-while always runs at least once:
  $n = 10;
  do {
      echo "run ";
  } while ($n < 5);
?>
1 2 3 run

Even though $n < 5 is false, the do-while printed "run" once because it checks the condition after running the body.

foreach (for arrays)

foreach is the easiest way to go through an array — no counter needed.

<?php
  $fruits = ["Apple", "Mango", "Banana"];
  foreach ($fruits as $fruit) {
      echo $fruit . " ";
  }
?>
Apple Mango Banana

For key-value arrays you can grab both: foreach ($ages as $name => $age) gives you the name and the age together each round.

break and continue

break leaves the loop early; continue skips just the current round.

<?php
  for ($i = 1; $i <= 5; $i++) {
      if ($i == 3) continue;  // skip 3
      if ($i == 5) break;     // stop before 5
      echo $i . " ";
  }
?>
1 2 4

3 is skipped by continue, and the loop stops at 5 because of break — so only 1, 2 and 4 print.

Common mistakes

  • Forgetting to update the counter. Missing $i++ gives an infinite loop.
  • Off-by-one errors. <= vs < changes how many times the loop runs — check the boundary.
  • Using for when foreach is cleaner. For arrays, foreach avoids index mistakes.
  • Changing the array while looping over it. This can cause skipped or repeated items.

Summary

  • Loops repeat code so you write less and do more.
  • for — when you know the count; while — repeat until a condition changes.
  • do-while always runs at least once; foreach is best for arrays.
  • break exits the loop; continue skips one round.
  • Always ensure the loop can reach its end, or it runs forever.

Next, store the values you loop over with arrays in PHP, or make choices inside loops with if-else in PHP.

Frequently Asked Questions

What is the difference between for and while loops?
Use a for loop when you know how many times to repeat (like 1 to 10). Use a while loop when you repeat until some condition changes and you may not know the count in advance. Both can do the same job; for is just tidier when counting.
What is the difference between while and do-while?
A while loop checks the condition first, so it may run zero times. A do-while loop runs the body once first and then checks, so it always runs at least once. Use do-while when the action must happen at least one time.
When should I use foreach?
Use foreach to go through every item of an array without managing an index yourself. It is the cleanest way to loop over arrays, and it works for both numbered and key-value (associative) arrays.
What causes an infinite loop in PHP?
An infinite loop happens when the condition never becomes false — usually because you forgot to change the counter (like missing $i++). Always make sure something inside the loop moves it toward ending.
What is the difference between break and continue?
break exits the loop completely and moves on. continue skips just the current turn and jumps to the next one. break stops everything; continue only skips one round.
← Back to PHP Tutorial
🔗

Share this topic with a friend

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

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

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

💻 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.
Next topicFunctions in PHP Your progress: 8 / 50 (16%) · Lesson 9 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free