php · level 1 · unit 4 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An if runs code only when its is true.
Picture a fork in the path with a sign: Is it Saturday? An if asks a condition like that. If the answer is yes, PHP runs the lines inside. If not, it skips past them.
PHP answers every condition with true or false. A value that can only be one of these two is a . To ask whether two values match exactly, use three equals signs, ===. One = puts a value in a box.
The condition goes in round brackets. The lines that belong to the if sit inside curly brackets, as one . Indent them with four spaces so people can see the block too.
<?php
$day = "Saturday";
if ($day === "Saturday") {
echo "Pancakes for breakfast!\n";
}
echo "Time to get dressed.\n"; Lines after the closing } sit outside the if, so they run every time.
guess first
Rain or shine? Guess what this shows.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
$weather = "sunny";
if ($weather === "rainy") {
echo "Take a coat.\n";
}
echo "Off you go!\n";
your turn
Your turn. Get past the robot door.
The robot only opens for its owner. Type your name in both blanks, spelled exactly the same, then press run.
fix it
Rome is not Paris. Fix the quiz!
- The answer was Rome, yet it says correct, and then You said Paris. Look at the sign in the
ifline. - One
=puts Paris into the box instead of asking a question. To ask whether two values match, you need three. - Change
=to===on line 3, then press run.
nailed it
your code can make choices now. :D
ifruns its block only when the condition istrue.- Use
===to ask whether two values match. - The block sits inside curly brackets, indented.
new words in your collection
- condition
- boolean
- block
This lesson isn’t saved. A free account remembers every lesson you finish.