Learn
Woah! We covered a lot in this lesson. Good work. Let’s review what we learned:
- Conditionals make it possible for programs to decide how to react to a wide variety of situations.
if
statements allow us to run a block of code if a condition is met.- The boolean data type is either the value
TRUE
orFALSE
and is the foundation of programmatic decision making. - We use
else
to include a block of code to run when the condition is not met. - Comparison operators evaluate a relationship between two operands and return a boolean value.
- The less than operator (
<
) - The less than or equal to operator (
<=
) - The greater than operator (
>
) - The greater than or equal to operator (
>=
) - The Identical operator (
===
) - The not identical operator
(!==)
- The less than operator (
- We can write conditionals with multiple
if
statements using theelseif
construction. - Instead of using a series of
if
statements when we want to compare a value, expression, or variable against many different possible values and run different code depending on which it matches, we can use aswitch
statement. - The keyword
break
tells the computer to break out of the switch statement, without it, it will fall through the rest of the switch executing all the code until it reaches abreak
or the end of the statement. - A ternary operator (
?:
) is shorthand conditional operator. It takes three operands (a condition to check, an expression to return if the condition isTRUE
, and an expression to return if the condition isFALSE
). - Any value or expression inside a condition will be converted to
TRUE
orFALSE
. We consider values that will convert toTRUE
to be truthy and values that will convert toFALSE
to be falsy. - We can get user input from the terminal with the
readline()
function.
That really is a lot… Take some time to practice and review! You’re doing great.
Instructions
We’ve provided you with a Harry Potter Sorting Hat Quiz program. Read through the code. It uses the skills you just learned! You can take the quiz yourself by running the program. You’ll need to enter php index.php
in the terminal and respond when prompted. If you’re feeling ambitious, customize and/or expand the quiz!
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.