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:
<?php $option = 1; switch ($option) { case 1: case 2: echo "The option is either 1 or 2."; break; } ?> |
chevron_right
filter_none
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; } ?> |
chevron_right
filter_none
Output:
The option is either 1 or 2
Reference: http://php.net/manual/en/control-structures.switch.php
Recommended Posts:
- Nested switch case
- Menu-Driven program using Switch-case in C
- Scala | Case Class and Case Object
- Nested switch statement in C++
- Remove consecutive alphabets which are in same case
- case command in Linux with examples
- Case insensitive search in JavaScript
- PHP | Check if all characters are lower case
- Convert string to title case in JavaScript
- Python | Toggle characters in words having same case
- How to prepare test case report for a Project?
- Python program to count upper and lower case characters without using inbuilt functions
- How to detect operating system on the client machine using JavaScript ?
- What is the difference between children and childNodes in JavaScript?
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.



