How can I make an OR instruction with a switch case? (Php)

How do I go about converting this if statement:

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

In the case of a switch, without having to write two switch statements?

+5
source share
4 answers
switch($argv[$i])
{
    case '-V':
    case '--version':
        $displayVersion = true;
    break;
}
+13
source

The direct translation will be as follows:

switch(in_array($argv[$i], array('-V', '--version'))){
    case true:
        $displayVersion = TRUE; break;
}

However, you can also do something like this, which is clearer.

switch($argv[$i]){
    case '-V':
    case '--version':
        $displayVersion = TRUE; break;
}

Depending on what you want to do, one liner may be more understandable, although it differs from the above code in that the variable will be set to false, if in_array($argv[$i], array('-V', '--version'))false. Given your variable name, I doubt it is bad.

$displayVersion = in_array($argv[$i], array('-V', '--version'));
+5
source
switch ($argv[$i])
{
    case '-V':
    case '--version':
        $displayVersion = true;
        break;
    case 'other':
        // do other stuff
        break;
    default:
        // your "else" case would go here
        break:
}
+3

PHP getopt, - , .

: ,

$options = getopt('V', array('version'));

if ($options['V'] || $options['version']) {
    $displayVersion = TRUE;
}

Please note that PHP 5.3 is required to run on Windows.

+2
source

All Articles