Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I do an OR statement with a Switch case? (PHP)

How would I go about converting this if statement:

for($i = 1; $i < $argc; $i++)
{
    ...
    if(in_array($argv[$i], array('-V', '--version')))
    {
        $displayVersion = TRUE;
    }
    ...
}

Into a switch case without needing to write two switch statements?

like image 597
Urda Avatar asked Feb 01 '10 14:02

Urda


People also ask

Can you use switch case in PHP?

PHP allows you to use number, character, string, as well as functions in switch expression.

How does a switch statement work PHP?

This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed.

How do you use a switch case statement?

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

Can you do an OR statement in a switch?

The logical OR operator (||) will not work in a switch case as one might think, only the first argument will be considered at execution time.


1 Answers

switch($argv[$i])
{
    case '-V':
    case '--version':
        $displayVersion = true;
    break;
}
like image 92
Tim Cooper Avatar answered Oct 18 '22 18:10

Tim Cooper