Best way to do a PHP switch with multiple values per case?

Viewed 213479

How would you do this PHP switch statement?

Also note that these are much smaller versions, the 1 I need to create will have a lot more values added to it.

Version 1:

switch ($p) { 
    case 'home': 
    case '': 
        $current_home = 'current';
    break; 

    case 'users.online': 
    case 'users.location': 
    case 'users.featured': 
    case 'users.new': 
    case 'users.browse': 
    case 'users.search': 
    case 'users.staff': 
        $current_users = 'current';
    break;

    case 'forum': 
        $current_forum = 'current';
    break; 
} 

Version 2:

switch ($p) { 
    case 'home': 
        $current_home = 'current';
    break; 

    case 'users.online' || 'users.location' || 'users.featured' || 'users.browse' || 'users.search' || 'users.staff': 
        $current_users = 'current';
    break;

    case 'forum': 
        $current_forum = 'current';
    break; 
} 

UPDATE - Test Results

I ran some speed test on 10,000 iterations,

Time1: 0.0199389457703 // If statements
Time2: 0.0389049446106 //switch statements
Time3: 0.106977939606 // Arrays

16 Answers

Nowadays you can do...

switch ([$group1, $group2]){
    case ["users", "location"]:
    case ["users", "online"]:
        Ju_le_do_the_thing();
        break;
    case ["forum", $group2]:
        Foo_the_bar();
        break;
}

Here's a ready ready-to-run set of code demonstrating the concept:

<?php

function show_off_array_switch(){
  $first_array = ["users", "forum", "StubbornSoda"];
  $second_array = ["location", "online", "DownWithPepsiAndCoke"];

  $rand1 = array_rand($first_array);
  $rand2 = array_rand($second_array);

  $group1 = $first_array[$rand1];
  $group2 = $second_array[$rand2];

  switch ([$group1, $group2]){
      case ["users", "location"]:
      case ["users", "online"]:
          echo "Users and Online";
          break;
      case ["forum", $group2]:
          echo "Forum and variable";
          break;
      default:
          echo "default";
  }
  echo "\nThe above result was generated using the array: \n" . print_r([$group1, $group2], true);
}

for ($i = 0; $i < 10; $i++){
  show_off_array_switch();
}

The following has (for one random run) output the following:

 Users and Online
The above result was generated using the array: 
Array
(
    [0] => users
    [1] => online
)
Users and Online
The above result was generated using the array: 
Array
(
    [0] => users
    [1] => online
)
default
The above result was generated using the array: 
Array
(
    [0] => users
    [1] => DownWithPepsiAndCoke
)
Users and Online
The above result was generated using the array: 
Array
(
    [0] => users
    [1] => location
)
Forum and variable
The above result was generated using the array: 
Array
(
    [0] => forum
    [1] => DownWithPepsiAndCoke
)
Forum and variable
The above result was generated using the array: 
Array
(
    [0] => forum
    [1] => DownWithPepsiAndCoke
)
Forum and variable
The above result was generated using the array: 
Array
(
    [0] => forum
    [1] => online
)
default
The above result was generated using the array: 
Array
(
    [0] => StubbornSoda
    [1] => location
)
Users and Online
The above result was generated using the array: 
Array
(
    [0] => users
    [1] => location
)
Users and Online
The above result was generated using the array: 
Array
(
    [0] => users
    [1] => location
)

maybe

        switch ($variable) {
        case 0:
            exit;
            break;
        case (1 || 3 || 4 || 5 || 6):
            die(var_dump('expression'));
        default:
            die(var_dump('default'));
            # code...
            break;
    }

You are simply looking for common patterns in strings. I would have thought a regular expression would be a more efficient way of doing this as PHP implements this with preg_match so little code to write and probably massively quicker. For example:

case preg_match('/^users./'):
// do something here
break;

From PHP documentation you can do that: https://www.php.net/manual/en/control-structures.switch.php#41767

$input = 'users.online';
$checked = null;
        
switch (true):
    case ($input === 'home'):
        $checked = 'ok 1';
        break;

    case (in_array($input , ['users.online', 'users.location', 'users.featured', 'users.browse', 'users.search', 'users.staff'])):
        $checked = 'ok 2';
        break;

    case ($input === 'forum'):
        $checked = 'ok 3';
        break;
endswitch;

echo $checked;
Related