0
0
PHPprogramming~10 mins

Switch statement execution in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a switch statement with the variable $color.

PHP
<?php
$color = 'red';
switch([1]) {
    case 'red':
        echo 'Stop';
        break;
    default:
        echo 'Go';
}
?>
Drag options to blanks, or click blank then click option'
Acolor
B$colour
C$color
Dred
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the dollar sign in front of the variable.
Using a misspelled variable name.
2fill in blank
medium

Complete the code to print 'Go' when the color is not 'red' or 'yellow'.

PHP
<?php
$color = 'green';
switch ($color) {
    case 'red':
        echo 'Stop';
        break;
    case 'yellow':
        echo 'Caution';
        break;
    [1]:
        echo 'Go';
}
?>
Drag options to blanks, or click blank then click option'
Aelse
Bdefault
Ccase 'green'
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'else' instead of 'default' in switch.
Forgetting to add a colon after the case or default.
3fill in blank
hard

Fix the error in the switch statement to correctly break after the 'yellow' case.

PHP
<?php
$color = 'yellow';
switch ($color) {
    case 'red':
        echo 'Stop';
        break;
    case 'yellow':
        echo 'Caution';
        [1]
    default:
        echo 'Go';
}
?>
Drag options to blanks, or click blank then click option'
Abreak
Bexit
Ccontinue
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' which is for loops, not switch.
Forgetting to add any statement to stop fall-through.
4fill in blank
hard

Fill both blanks to create a switch that prints 'Weekend' for 'Saturday' and 'Sunday', and 'Weekday' otherwise.

PHP
<?php
$day = 'Sunday';
switch([1]) {
    case 'Saturday':
    case [2]:
        echo 'Weekend';
        break;
    default:
        echo 'Weekday';
}
?>
Drag options to blanks, or click blank then click option'
A$day
B'Sunday'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without the dollar sign.
Using the variable instead of the string for the second case.
5fill in blank
hard

Fill all three blanks to create a switch that prints 'Low', 'Medium', or 'High' based on the $level variable.

PHP
<?php
$level = 2;
switch([1]) {
    case 1:
        echo [2];
        break;
    case 2:
        echo [3];
        break;
    default:
        echo 'High';
}
?>
Drag options to blanks, or click blank then click option'
A$level
B'Low'
C'Medium'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the strings in echo.
Using the wrong variable name in the switch.