Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Medium
  • Last Updated : 11 Nov, 2018

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :