Loops

A loop repeats a block of PHP until its condition says stop. The four loops of the language differ in where the condition sits and what walks through the data.

The for loop

The for loop bundles three parts in its header: an initialization, a condition checked before every pass, and a step that runs after every pass. It fits counting over a known range, where the start, the end, and the step are all visible in one line.

<?php for ($i = 0; $i < 5; $i++) { echo $i, PHP_EOL; }

php for.php

0
1
2
3
4

The counter stops at 4, because the condition is checked before the pass that would print 5.

Foreach over arrays

The foreach loop walks an array without a manual index, so there is no counter to misplace and no end to overshoot. Add $key => $value when the index or the key matters, not just the element.

<?php $tags = ["php", "html", "css"]; foreach ($tags as $index => $tag) { echo $index . ": " . $tag, PHP_EOL; }

php foreach.php

0: php
1: html
2: css

The keys are the positions of the elements, counted from zero, and the dot joins each one with its label. Without the key part the loop would still visit every element, and only the numbers would be lost.

while and do-while

The while loop checks its condition first, so the body may run zero times. The do-while loop checks last, so the body always runs at least once. Both need the condition to change inside the body, or the loop never ends.

<?php $left = 3; while ($left > 0) { echo $left, PHP_EOL; $left--; } do { echo "do-while ran", PHP_EOL; $left++; } while ($left < 0);

php while.php

3
2
1
do-while ran

The countdown ends with $left at zero, which is already not less than zero, yet the do-while body still runs once and prints its line before the condition is tested.

break and continue

The break statement exits the loop at once, while continue skips the rest of the current pass and jumps to the next one. Together they express search, skip, and stop rules without extra flag variables.

<?php $tags = ["php", "html", "css"]; foreach ($tags as $tag) { if ($tag === "html") { continue; } if ($tag === "css") { break; } echo $tag, PHP_EOL; }

php control.php

php

The second element is skipped by continue, and the third ends the loop before its echo, so only the first tag reaches the output.

Exercises

  1. Change the condition of for.php to $i <= 5 and run it. How many lines print now?
  2. Add the sum of the numbers 1 to 100 to the for loop and print it once after the loop.
  3. Remove the break from control.php and run it. Which tags print now, and why?
  4. Write a do-while loop that prints its line although the condition is false from the start, then rewrite it as a while loop and compare the output.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which part of a for header runs after every pass?

    Answer

    The step

    The third part, usually $i++, runs after each pass and before the condition is checked again.

  2. What does foreach ($items as $key => $value) give you that foreach ($items as $value) does not?

    Answer

    The key or index of each element

    The arrow form binds both the key and the value of the current element.

  3. Which loop always runs its body at least once?

    Answer

    do-while

    The do-while tests its condition after the body, so the body runs before the first test.

  4. What does continue do inside a loop?

    Answer

    It skips the rest of the current pass and starts the next one

    continue jumps to the next iteration; only break leaves the loop altogether.

  5. What does this program print? <?php for ($i = 1; $i <= 3; $i++) { echo $i; }

    Answer

    123

    The counter starts at 1, prints while it is at most 3 and stops after 3, so the output is 123.

  6. What does this program print? <?php $sum = 0; for ($i = 1; $i <= 4; $i++) { $sum += $i; } echo $sum;

    Answer

    10

    The loop adds 1, 2, 3 and 4, and their sum is 10.

  7. What does this program print? <?php foreach (["a", "b"] as $k => $v) { echo $k . $v; }

    Answer

    0a1b

    The keys are 0 and 1, and each is glued to its value, which gives 0a1b.

  8. What does this program print? <?php $i = 5; do { echo $i; } while ($i < 3);

    Answer

    5

    The do-while body runs before the first test, so 5 prints once and the false condition ends the loop.

  9. What does this program print? <?php foreach ([1, 2, 3, 4] as $n) { if ($n == 2) { continue; } echo $n; }

    Answer

    134

    Only the pass with 2 is skipped, so the other three numbers print.

  10. What does this program print? <?php foreach ([1, 2, 3, 4] as $n) { if ($n == 3) { break; } echo $n; }

    Answer

    12

    The loop ends at 3 before printing it, so only the two numbers before it reach the output.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev