×

php · level 1 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A does the same job again and again.

A loop runs the same lines again and again. A for loop is the one to use when you know how many times: run three laps, clap ten times.

Its round brackets hold three parts, split by ;. Where to start, $lap = 1. When to keep going, $lap <= 3. What to do after each go, $lap++, which adds one.

$lap is a : it goes up by one each time round, and you can use it inside the loop. The block in the curly brackets runs once for every go.

<?php
for ($lap = 1; $lap <= 3; $lap++) {
    echo "Lap $lap: run!\n";
}
echo "Finished!\n";

$lap++ is a short way to write $lap = $lap + 1.