PHP switch case more than one value in the case

PHP switch case more than one value in the case

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Switch Case More Than One Value in the Case

You're running into a common pattern where you need to map specific integer inputs to distinct string outputs, but the mapping isn't one-to-one. In your example, values 1 and 2 both resolve to 'Weekly', while 4 and 5 resolve to 'Quarterly'. Writing out every single case with an identical body block is verbose and makes maintenance difficult; if you later decide that case 3 should also be 'Monthly', you have to remember to update three lines.

The straightforward syntax in PHP does not support a comma-separated list of values within a single case label like it does in some other languages (e.g., JavaScript or C). You cannot write:

case 1, 2:
    // This is invalid PHP syntax

If you try this, the parser will throw a syntax error because the comma is interpreted as part of a different construct, not a logical OR for case labels. The language specification restricts each case to a single expression or a list of expressions separated by commas only when those expressions are part of a larger statement structure, not as a direct label modifier in a switch.

To achieve the logic you want without duplicating code, there are three primary approaches: using multiple case labels stacked vertically, falling through cases intentionally, or restructuring the logic entirely with arrays or match statements.

Stacking Case Labels

The most idiomatic way to handle this in standard PHP is to simply stack the case statements that share the same behavior. While it looks like duplication, the compiler treats them as a single logical block due to implicit fall-through. You don't need to break after every line; you only break once at the end of the shared logic:

switch ($var2) {
    case 1:
    case 2:
        $var3 = 'Weekly';
        break;

    case 3:
        $var3 = 'Monthly';
        break;

    case 4:
    case 5:
        $var3 = 'Quarterly';
        break;
}

This is efficient because the switch statement jumps to the first matching label and executes until it hits a break, return, or the end of the switch. By omitting the break between case 1 and case 2, you effectively create a single execution path for both inputs. This pattern scales well even if you have dozens of cases grouped together.

Using an Array Lookup

If your input space is dense (e.g., 1 through 10) or if the logic becomes more complex than simple string assignment, consider switching from a switch to a lookup array. Arrays in PHP are associative and can map integers directly to strings. This approach eliminates the need for control flow statements entirely and often results in cleaner, faster code:

$periods = [
    1 => 'Weekly',
    2 => 'Weekly',
    3 => 'Monthly',
    4 => 'Quarterly',
    5 => 'Quarterly',
    // Add defaults or handle missing keys if necessary
];

$var3 = $periods[$var2] ?? 'Invalid Period';

This method is generally faster than a switch statement for large datasets because it avoids the jump-table overhead and branching logic. It also makes it trivial to add new mappings or change values without altering control flow structures. If you are using Laravel, this pattern aligns well with how many configuration lookups are handled in application code.

PHP 8 Match Expressions

If you are running PHP 8.0 or later, the match expression offers a more robust alternative to switch. Unlike switch, match is exhaustively checked at compile time, meaning it will error if no default case is provided and all possible values aren't covered. While match still doesn't support comma-separated cases directly in the label, its strictness prevents accidental fall-through bugs:

$var3 = match ($var2) {
    1, 2 => 'Weekly', // Syntax error in PHP as of 8.x for multiple values in one arm?
    // Actually, match does not support multiple values in one arm either.
};

Wait, there is a nuance here. Even in match, you cannot use 1, 2 in a single arm; it must be stacked like switch. However, the stackable nature of match arms combined with its strict return type checking often makes it preferable for complex logic where you need to guarantee every branch is handled.

Final Recommendation

For your specific scenario, stacking case labels is the standard PHP solution and works perfectly across all versions. If you anticipate frequent changes or if the mapping grows beyond a handful of values, refactor to an array lookup. It reduces cognitive load and removes the visual clutter of repeated break statements. Remember that while JavaScript allows comma-separated cases, PHP relies on fall-through behavior for grouping, making the stacked case pattern the correct and expected way to handle multiple values per outcome.

Our network