Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

The option is either 1 or 2.

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




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


Output:

The option is either 1 or 2

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


Last Updated : 11 Nov, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads