Do shorthand switch statements exist (in PHP)?
Asked Answered
T

2

7

I want to compare the same variable (or expression) with many different values, and return a different value depending on which value it equals to. I want to do this inline or shorthand, as is possible with an if statement.

Take the following switch statement:

switch($color_name) {
    case 'red':
    case 'blue':
        $color_type = handlePrimaryColor($in);
        break;
    case 'yellow':
    case 'cyan':
        $color_type = handleSecondaryColor($in);
        break;
    case 'azure':
    case 'violet':
        $color_type = handleTertiaryColor($in);
        break;
    default:
        $color_type = null;
        break;
}

I don't like writing $color_type = in every case and would like to find a way to do this with less code.

I could do it with some form of shorthand syntax. Below, I use a shorthand if statement to assign a value to the variable in the same place where it is first being declared:

$color_type = $color_name == 'red' || $color_name == 'blue'
    ? handlePrimaryColor($color_name)
    : ($color_name == 'yellow' || $color_name == 'cyan'
        ? handleSecondaryColor($color_name)
        : ($color_name == 'azure' || $color_name == 'violet'
            ? handleTertiaryColor($color_name)
            : null
        )
    );

This method doesn't require declaring the variable within each construct, but gives me 2 new problems instead:

  • I now have to write a new OR condition for each color
  • Every group of conditions adds an extra level of nesting

My question: Is there a method which allows me to directly assign a value to a variable using shorthand syntax that behaves like a switch?

If there isn't, I would be interested in learning why that limitation exists.

Thomson answered 26/9, 2014 at 14:45 Comment(4)
I bet your colleagues love youBobsleigh
Why is that? I am just interested in learning if it's possible to translate my switch statement into a shorthand version. In reality I don't expose my colleagues to multi-level nested inline if statements. The subject and content of my code in the question only serve as an example.Thomson
It was just a light hearted comment - i have encountered code like that in the wild before, it was a depressing experience!Bobsleigh
Hey taken lightly, I was only afraid someone might think I produce such code, while the purpose of my question is to avoid it :)Thomson
S
7

Since PHP 8.0 there is a shorthand switch available which is called match

$colorType = match($color) {
    'red', 'blue' => handlePrimaryColor($in),
    'yellow', 'cyan' => handleSecondaryColor($in),
    'azure', 'violet' => handleTertiaryColor($in),
    default => null
};
Sarcenet answered 17/11, 2021 at 15:25 Comment(2)
The semicolon should be at the end of the block, after curly bracket closed.Koslo
And there, my 8 year old question finally gets an accepted answer :)Thomson
H
5

Too much work. Use a dispatch table.

$color_dispatch = Array(
  'red' => 'handlePrimaryColor',
  'blue' => 'handlePrimaryColor',
   ...
);

$color_type = null;
if (array_key_exists($color_name, $color_dispatch))
{
  $color_type = $color_dispatch[$color_name]($in);
}
Hiroshima answered 26/9, 2014 at 14:51 Comment(4)
Nice approach, but I think you want isset($color_dispatch[$color_name]) or array_key_exists. Check for the key not the value.Lapin
@AbraCadaver: Indeed. Fixed.Hiroshima
I'm sorry, but the intention of my question was to find out if such a method exists (like shorthand if statements exist) or learn about the limitations why such a thing can't/doesn't exist. I'm not looking for alternatives, but just curious.Thomson
It doesn't exist because everybody uses dispatch tables instead.Hiroshima

© 2022 - 2024 — McMap. All rights reserved.