PHP Switch Case Example

Choose between many branches with switch and match in PHP. Edit the code below and press Run to execute it with real PHP in your browser.

Code Editor
Output
Start of the week

How this switch case example works

switch compares the value against each case using loose comparison (==). Execution continues until a break, which is why "Saturday" and "Sunday" can share one branch. Forgetting break makes the code "fall through" into the next case.

The PHP 8 match expression

match uses strict comparison, never falls through, returns a value and throws an UnhandledMatchError if nothing matches and there is no default.

<?php
$day = "Sunday";
echo match ($day) {
    "Monday" => "Start of the week",
    "Friday" => "End of the week",
    "Saturday", "Sunday" => "Weekend",
    default => "Midweek",
};

Output:

Weekend