How to use a switch case 'or' in PHP

alex picture alex · Oct 16, 2008 · Viewed 287.3k times · Source

Is there a way of using an 'OR' operator or equivalent in a PHP switch?

For example, something like this:

switch ($value) {

    case 1 || 2:
        echo 'the value is either 1 or 2';
        break;
}

Answer

user19302 picture user19302 · Oct 16, 2008
switch ($value)
{
    case 1:
    case 2:
        echo "the value is either 1 or 2.";
    break;
}

This is called "falling through" the case block. The term exists in most languages implementing a switch statement.