×

php · level 1 · unit 5 · lesson 3

  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

Put an if inside a loop to check every number.

A loop can make a decision on every go. Put an if inside the loop's block, and PHP asks the question again each time round.

% is perfect for this. A number is even when dividing by 2 leaves nothing over, so $house % 2 === 0 is true for 2, 4 and 6.

To count the matches, keep a . Start it at 0 before the loop, then add one inside the if. After the loop, the tally holds the total.

<?php
$left = 0;
for ($house = 1; $house <= 6; $house++) {
    if ($house % 2 === 0) {
        $left++;
    }
}
echo "Letters for the left side: $left\n";

Notice the two closing } marks: the first ends the if, the second ends the loop.