Skip to content

Exercise 5 of 10 · Loops

Launch Countdown

What you will make

A countdown from 5 down to 1, printed one number per line, followed by a liftoff message.

The one new idea: a for loop repeats a block a set number of times

Repetition is what makes a computer worth using. A for loop lets a handful of lines do work that would otherwise take one line per number — the loop in this exercise is doing the job of five separate echo statements.

Go straight to the code ↓

Repeating with for

A for loop repeats a block of code a set number of times, controlled by three parts written in its header, separated by semicolons: a starting point, a condition checked before every pass, and an update that runs after every pass. As long as the condition stays true, the loop keeps going; the moment it's false, the loop stops — including on the very first check, if it's false right from the start.

Before loops, repeating an action five times meant writing five separate echo lines by hand. A for loop does the same work with one small header and a single body, and the body doesn't change even if you later need fifty repetitions instead of five.

A worked example

Here's a different loop, counting up instead of down, so the pattern is visible without today's answer:

PHP
<?php
for ($i = 1; $i <= 4; $i++) {
    echo "Lap $i\n";
}
echo "Finished!\n";

Running it prints:

Output
Lap 1
Lap 2
Lap 3
Lap 4
Finished!

$i starts at 1, the loop keeps going while $i <= 4, and $i++ adds one after every pass. Once $i becomes 5, the condition is false, the loop stops, and the line after the closing brace — Finished! — runs exactly once, outside the loop entirely.

Your turn

The starter program is supposed to count down from 5 to 1, printing each number, then print Liftoff!. Right now only Liftoff! prints — the countdown never appears at all.

Look at the loop's condition. $i starts at 5 and counts down with $i--, so the loop needs to keep running for as long as $i is still large enough, not stop as soon as it's already small. Fix the comparison operator in the condition, then press Run.

If something goes wrong

If nothing from inside the loop prints, the condition is almost certainly false from the very first check — PHP tests a for loop's condition before the first pass, so a condition that starts out false skips the body completely, every single time. If the countdown runs forever, or goes well past 1, check that $i-- is still there in the loop header, updating $i after every pass, and that you haven't accidentally reversed it into $i++.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Writing a condition that's already false at the start
PHP checks a for loop's condition *before* the very first pass — if it's false immediately, the loop body never runs even once, and nothing inside it prints.
Mixing up <= and >= when counting downward
Counting up usually pairs with <= (keep going while small enough) and $i++. Counting down needs the opposite direction, >= (keep going while still large enough), paired with $i--.
Forgetting that the loop variable updates automatically
$i-- at the end of the for(...) header runs after every pass without needing to be repeated inside the braces — writing it again inside the loop body would count down twice as fast.

Want a blank editor instead? Open the PHP playground.