Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to use a switch case ‘or’ in PHP?

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;
}
?>


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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments