PHP Switch

PHP Switch

In PHP, the a switch statement is used to execute one out of multiple blocks of code based on the value of an expression. It is a cleaner alternative to using multiple if, elseif, and else statements when you need to compare the same variable or expression to different values.

Syntax of switch

switch (expression) { case value1: // Code to be executed if expression == value1; break; case value2: // Code to be executed if expression == value2; break; case value3: // Code to be executed if expression == value3; break; default: // Code to be executed if expression doesn't match any case; }
  • expression: The value or variable that is being evaluated.
  • case: The possible values that expression might match.
  • break: Ends the switch statement once a match is found. Without it, all cases below the matching one will also be executed (this is called "fall-through").
  • default: The block of code that runs if no case matches. This is optional.

Example 1: Basic Switch Statement

$day = "Monday"; switch ($day) { case "Monday": echo "Start of the work week."; break; case "Friday": echo "Almost the weekend!"; break; case "Saturday": case "Sunday": echo "It's the weekend!"; break; default: echo "It's a weekday."; }

Output:

Start of the work week.
  • The switch evaluates the $day variable. It finds that $day is "Monday", so it executes the code block for that case and then stops because of the break statement.
  • The case "Saturday": and case "Sunday": are grouped together, so both of these will output the same code if either "Saturday" or "Sunday" is the value.

Example 2: Without break (Fall-through Behavior)

If you don't use the break statement, the code will "fall through" and execute the next case, even if the previous case matched. This can be useful in some cases, but it can also lead to unexpected behavior if not carefully managed.

$score = 85; switch ($score) { case 90: echo "Grade A"; break; case 80: echo "Grade B"; // No break; this will fall through to the next case case 70: echo "Grade C"; break; default: echo "No grade"; }

Output:

Grade BGrade C
  • Since $score is 85, no match is found, so it moves to the default case, and No grade will be printed.

Example 3: Using switch with a String

You can use a switch statement with strings as well as numbers.

$fruit = "apple"; switch ($fruit) { case "apple": echo "It's an apple."; break; case "banana": echo "It's a banana."; break; case "orange": echo "It's an orange."; break; default: echo "Unknown fruit."; }

Output:

It's an apple.

Example 4: Using default with switch

The default case in a switch statement is optional but can be used to handle cases where no specific case is matched.

$color = "blue"; switch ($color) { case "red": echo "Color is red."; break; case "green": echo "Color is green."; break; default: echo "Color is not red or green."; }

Output:

Color is not red or green.
  • Since the $color is "blue", and there is no matching case for it, the code inside the default block is executed.

Example 5: Switch with Multiple Values (Grouping Cases)

You can group multiple case values together so that they share the same block of code.

$day = "Saturday"; switch ($day) { case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday": echo "It's a weekday."; break; case "Saturday": case "Sunday": echo "It's the weekend!"; break; default: echo "Invalid day."; }

Output:

It's the weekend!
  • Here, the cases for "Saturday" and "Sunday" are grouped together and output "It's the weekend!". Similarly, weekdays are grouped together in one block.

Example 6: Using switch with a Range of Values

Although you can't directly compare a variable with a range in a switch statement, you can still use if or elseif statements inside the case blocks to handle ranges.

$score = 85; switch (true) { case ($score >= 90): echo "Grade A"; break; case ($score >= 80): echo "Grade B"; break; case ($score >= 70): echo "Grade C"; break; default: echo "Grade F"; }

Output:

Grade B
  • Here, the switch (true) pattern is used to evaluate conditions based on ranges. The first true condition is $score >= 80, so "Grade B" is printed.

Key Points to Remember:

  • switch works best when you have multiple possible values to compare against the same expression.
  • break is used to stop the execution once a match is found; without it, the next cases will also be executed (fall-through behavior).
  • default is optional, but it is useful for catching any unmatched values.
  • You can group multiple case labels together to share the same block of code.

The switch statement is especially useful when you have many conditions that need to be checked against a single value or expression. It can make your code cleaner and more readable when dealing with complex conditionals.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close