Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch-Case with multiple Values

I'm writing a code at the moment where I want to install network-shared printers automatically. Which printer the script should install depends on whether a user works for example in Sales or HR. I wanted to solve this problem with a switch statement, but the problem is that it always matches the first value.

I tried several combinations of continue- or break-points, but none of them lead to my desired result.

$a = "HR"

switch ($a) {
    {"Marketing", "Sales"}     { "1" }
    {"Sales Department", "HR"} { "2" }
    "EDV"                      { "3" }
}

Output:

1
2

Normally, the console output should be "2", but it is "1" "2".

like image 312
SSIO Avatar asked Jul 17 '19 09:07

SSIO


People also ask

Can case in switch statement have multiple values?

The switch can includes multiple cases where each case represents a particular value. Code under particular case will be executed when case value is equal to the return value of switch expression. If none of the cases match with switch expression value then the default case will be executed.

Can switch have 2 arguments?

Use the fall-through feature of the switch statement to use a switch case with multiple arguments in JavaScript. A matched case will run until a break (or the end of the switch statement) is found.


1 Answers

Change the condition block to:

{$_ -in "Marketing", "Sales"}

This way both terms will match the switch case

like image 188
Mathias R. Jessen Avatar answered Oct 21 '22 04:10

Mathias R. Jessen