PHP Switch

The switch statement in PHP is used to execute different blocks of code based on the value of an expression. This makes it an efficient alternative to using multiple if...else statements, especially when comparing one expression against multiple cases.

Tutorials dojo strip



Syntax

switch (expression) {
  case label1:
    // Code block
    break;
  case label2:
    // Code block
    break;
  case label3:
    // Code block
    break;
  default:
    // Code block
}




How It Works

  1. The expression is evaluated once.
  2. Its value is compared with each case label.
  3. If a match is found, the corresponding block of code is executed.
  4. The break keyword exits the switch block to prevent the execution of the next cases.
  5. If no match is found, the default block runs, if specified.




Example: Choosing a Motorcycle Brand

Here’s an example where a user specifies their preferred motorcycle brand. The program outputs a message based on the choice:

$preferredBrand = "Kawasaki";

switch ($preferredBrand) {
  case "Kawasaki":
    echo "You love Kawasaki's speed and performance!";
    break;
  case "Ducati":
    echo "You appreciate Ducati's style and heritage!";
    break;
  case "Harley-Davidson":
    echo "You're drawn to Harley-Davidson's iconic legacy!";
    break;
  default:
    echo "It seems you're into an uncommon motorcycle brand!";
}




The break Keyword

The break keyword stops further case evaluations once a match is found. Without it, subsequent case blocks are executed even if they don’t match the expression.

Example: Omitting break

If we remove the break in one of the cases, here’s what happens:

$preferredBrand = "Kawasaki";

switch ($preferredBrand) {
  case "Kawasaki":
    echo "You love Kawasaki's speed and performance!";
  case "Ducati":
    echo "You appreciate Ducati's style and heritage!";
    break;
  case "Harley-Davidson":
    echo "You're drawn to Harley-Davidson's iconic legacy!";
    break;
  default:
    echo "It seems you're into an uncommon motorcycle brand!";
}

Here, if the preferredBrand is “Kawasaki”, both the “Kawasaki” and “Ducati” blocks will execute, producing unintended behavior.




The default Keyword

The default block executes when no case matches the expression. While it’s often the last block, it can be placed elsewhere (not recommended) but must include a break to avoid unintended results.

Example: Handling Unmatched Cases

$day = 4;

switch ($day) {
  case 1:
    echo "Start your week with energy!";
    break;
  case 7:
    echo "Sunday is for rest and relaxation!";
    break;
  default:
    echo "Another productive weekday ahead!";
}




Common Code Blocks

If multiple cases should trigger the same block of code, they can be grouped:

Example: Grouping Cases

$day = 5;

switch ($day) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    echo "The week is in full swing!";
    break;
  case 6:
  case 7:
    echo "Time to enjoy the weekend!";
    break;
  default:
    echo "Something seems off about this day.";
}

Tutorials dojo strip
Scroll to Top