Menu
Coddy logo textTech

Switch Statement

Part of the Fundamentals section of Coddy's PHP journey — lesson 32 of 71.

When you need to compare a single variable against many specific values, the switch statement offers a cleaner alternative to long if-elseif chains.

The switch statement checks a variable once and then matches it against different case values:

<?php
$day = "Monday";

switch ($day) {
    case "Monday":
        echo "Start of the week";
        break;
    case "Friday":
        echo "Almost weekend";
        break;
    case "Saturday":
    case "Sunday":
        echo "Weekend!";
        break;
    default:
        echo "Regular day";
}
?>

PHP compares $day against each case. When it finds a match, it runs that block's code.

The break statement is crucial—it tells PHP to exit the switch after executing the matched case. Without it, PHP would continue running all the code below, even from other cases.

Notice how Saturday and Sunday share the same code by stacking cases together. The default case acts like an else—it runs when no other case matches.

<?php
$grade = "B";

switch ($grade) {
    case "A":
        echo "Excellent!";
        break;
    case "B":
        echo "Good job!";
        break;
    default:
        echo "Keep trying!";
}
?>

Use switch when comparing one variable against multiple exact values. For complex conditions or ranges, stick with if-elseif statements.

challenge icon

Challenge

Easy

Read a string command from input representing a traffic light color.

Use a switch statement to determine the appropriate action based on the color:

  • If the command is red, print Stop
  • If the command is yellow, print Slow down
  • If the command is green, print Go
  • If the command is flashing red or flashing yellow, print Proceed with caution
  • For any other value, print Invalid signal

Example:

If the input is green, the output should be:

Go

If the input is flashing red, the output should be:

Proceed with caution

If the input is blue, the output should be:

Invalid signal

Cheat sheet

The switch statement provides a cleaner way to compare a single variable against multiple specific values:

<?php
$day = "Monday";

switch ($day) {
    case "Monday":
        echo "Start of the week";
        break;
    case "Friday":
        echo "Almost weekend";
        break;
    default:
        echo "Regular day";
}
?>

The break statement exits the switch after executing the matched case. Without it, PHP continues running code from subsequent cases.

Multiple cases can share the same code by stacking them:

<?php
switch ($day) {
    case "Saturday":
    case "Sunday":
        echo "Weekend!";
        break;
}
?>

The default case runs when no other case matches, similar to an else statement.

Use switch statements for comparing one variable against multiple exact values. For complex conditions or ranges, use if-elseif statements instead.

Try it yourself

<?php
// Read input
$command = trim(fgets(STDIN));

// TODO: Write your code below using a switch statement
// Determine the appropriate action based on the traffic light color


?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals