How to use a switch case ‘or’ in PHP?

Problem: How to use a switch case ‘or’ in PHP?
Solution: This can be achieved in 2 ways. They are as follows:
Method 1: Without using break keyword in one of the switch case.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$option = 1;
switch ($option)
{
    case 1:
    case 2:
        echo "The option is either 1 or 2.";
    break;
}
?>

chevron_right


Output:

The option is either 1 or 2.

Method 2: Using or operator in switch case.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$option = 1;
switch ($option) {
    case ($option == 1 || $option == 2):
        echo 'The option is either 1 or 2';
        break;
}
?>

chevron_right


Output:

The option is either 1 or 2

Reference: http://php.net/manual/en/control-structures.switch.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.