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
expression
: The value or variable that is being evaluated.case
: The possible values thatexpression
might match.break
: Ends theswitch
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 nocase
matches. This is optional.
Example 1: Basic Switch Statement
Output:
- 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 thebreak
statement. - The
case "Saturday":
andcase "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.
Output:
- Since
$score
is 85, no match is found, so it moves to thedefault
case, andNo grade
will be printed.
Example 3: Using switch
with a String
You can use a switch
statement with strings as well as numbers.
Output:
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.
Output:
- Since the
$color
is "blue", and there is no matching case for it, the code inside thedefault
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.
Output:
- 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.
Output:
- 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.