php · level 1 · unit 4 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
adds more paths to the fork.
Some questions have more than two answers. What should you wear? It depends how cold it is. elseif adds another condition to check when the ones above it were false.
PHP checks from the top down and stops at the first condition that is true. Only that block runs. Add as many elseif parts as you like, then an else to catch the rest.
Every gives true or false. < means less than, > more than, <= less than or equal to, >= more than or equal to, and !== means not equal to.
<?php
$temperature = 12;
if ($temperature < 5) {
echo "Wear a big coat.\n";
} elseif ($temperature < 15) {
echo "A jumper will do.\n";
} In <= and >= the = always comes second, never first.
guess first
Big slide or small slide? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
$height = 120;
if ($height > 120) {
echo "Hop on the big slide!\n";
} elseif ($height >= 100) {
echo "Try the small slide.\n";
}
your turn
Your turn. How full is your battery?
Choose a battery level and a comparison sign, like < or >=, so that the elseif message shows.
fix it
The top score gets no gold star. Fix the order.
- PHP checks from the top and stops at the first
true. Which check istruefirst for 95? - 95 is at least 50, so the gold star never gets a chance. The bigger check needs to come first.
- Swap them: check
$score >= 90first with its gold star, then$score >= 50with its pass message.
nailed it
your code can pick from many paths. :D
elseifchecks a new condition when the ones above werefalse.- PHP stops at the first
truecondition. <,>,<=,>=,===and!==all givetrueorfalse.
new words in your collection
- elseif
- comparison
This lesson isn’t saved. A free account remembers every lesson you finish.