case
As a PHP developer, you may have used the "switch" statement to handle multiple conditions in your code. The "case" keyword is a key component of the "switch"
As a PHP developer, you may have used the switch statement to handle multiple conditions in your code. The case keyword is a key component of the switch statement, allowing you to define specific actions to take based on the value of a variable. This page covers the syntax of case, how fall-through and break work, the loose-comparison gotcha, the switch (true) pattern for ranges, and how case relates to if/elseif chains.
Syntax
The "case" keyword is used to define a specific action to take based on the value of a variable in a "switch" statement. Here is the basic syntax for using the "case" keyword in PHP:
The PHP syntax of case
<?php
switch ($variable) {
case $value1:
// Code to execute if $variable equals $value1
break;
case $value2:
// Code to execute if $variable equals $value2
break;
default:
// Code to execute if $variable does not equal any of the specified values
}In this example, the "case" keyword is used to define specific actions to take based on the value of the "$variable" variable. The break statement is crucial: it terminates the switch block and prevents "fall-through" to the next case. Without it, PHP will continue executing code in subsequent cases until it hits a break or the end of the switch block. Additionally, note that PHP's switch uses loose comparison (==) by default, meaning case 0 will also match "0" or false. Use strict comparison logic if exact type matching is required.
Examples
Let's look at some practical examples of how the "case" keyword can be used:
Example of PHP case
<?php
// Example 1
$day = "Monday";
switch ($day) {
case "Monday":
echo "Today is Monday." . PHP_EOL;
break;
case "Tuesday":
echo "Today is Tuesday.";
break;
case "Wednesday":
echo "Today is Wednesday.";
break;
default:
echo "Today is not Monday, Tuesday, or Wednesday.";
}
// Output: Today is Monday.
// Example 2
$grade = 85;
switch (true) {
case $grade >= 90:
echo "A";
break;
case $grade >= 80:
echo "B";
break;
case $grade >= 70:
echo "C";
break;
case $grade >= 60:
echo "D";
break;
default:
echo "F";
}
// Output: BExample 1 matches a string value directly. Example 2 uses the switch (true) pattern: each case holds a boolean expression, and the first one that evaluates to true runs. This is the idiomatic way to handle ranges in a switch, since plain case only compares for equality.
Grouping cases (intentional fall-through)
Because a case without a break falls through to the next one, you can stack labels to run the same block for several values:
<?php
$day = "Sat";
switch ($day) {
case "Sat":
case "Sun":
echo "Weekend";
break;
default:
echo "Weekday";
}
// Output: WeekendHere "Sat" matches the first label, falls through the empty "Sun" case, and runs the shared block. This is the one situation where omitting break is deliberate rather than a bug.
Benefits
Using the "case" keyword has several benefits, including:
- Simplified code: The "case" keyword allows you to create shorter, more concise code that is easier to read and understand.
- Improved readability: The "switch" statement provides a cleaner structure than long chains of "if/elseif" statements when comparing a single variable against multiple values.
- Increased flexibility: The "case" keyword allows you to define specific actions to take based on the value of a variable, making your code more adaptable to different scenarios.
Conclusion
The case keyword lets you define specific actions based on the value of a variable inside a switch statement. Remember the two things that trip people up: add break to every case unless you intend to fall through, and watch out for loose comparison when matching numbers against strings.
Related topics
- PHP
switchstatement — the full structure thatcaselives inside. switch— keyword reference forswitch.- PHP
if...else...elseif— when a chain of conditions reads better than aswitch. - PHP operators — how
==(loose) and===(strict) comparison differ.