Control Flow: Switch
Switch Syntax

A switch statement is similar to an if / elif / else statement in that you can check multiple conditions. Here's what it looks like:

$myNum = 2;

switch ($myNum) {
    case 1:
        echo "1";
    case 2:
        echo "2";
    case 3:
        echo "3";
        echo "None of the above";
  1. A switch statement is made up of the switch keyword, a variable to check, and a pair of curly braces { }. Here we check the value of $myNum.
  2. Then we have a case block for each comparison. For example case 1: echo "1"; break; checks whether $myNum is equal to 1. If yes, it echos "1", and uses break to exit the switch statement.
  3. Otherwise, the next case block runs.
  4. If all cases return false, the default case gets executed.

On line 10, there's a switch statement

  1. Fill the __ spots with the correct code. Check out the example above
  2. Add the default case.
Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

You should replace the two __ with either the word break or case. Check out the example above to see where the default case and break should go.

View Preview