每种情况下使用多个值进行PHP切换的最佳方法?

Jas*_*vis 67 php switch-statement

你会怎么做这个PHP switch语句?

另请注意,这些是更小的版本,我需要创建的1将添加更多的值.

版本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; 
} 
Run Code Online (Sandbox Code Playgroud)

版本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; 
} 
Run Code Online (Sandbox Code Playgroud)

更新 - 测试结果

我在10,000次迭代中运行了一些速度测试,

Time1:0.0199389457703 //如果语句
Time2:0.0389049446106 // switch语句
Time3:0.106977939606 //数组

too*_*php 51

对于你有一个未知字符串的任何情况,你需要弄清楚它匹配的其他字符串中的哪一个,唯一的解决方案是,当你添加更多的项目时不会变慢,是使用一个数组,但所有可能的字符串作为键.因此,您的交换机可以替换为以下内容:

// used for $current_home = 'current';
$group1 = array(
        'home'  => True,
        );

// used for $current_users = 'current';
$group2 = array(
        'users.online'      => True,
        'users.location'    => True,
        'users.featured'    => True,
        'users.new'         => True,
        'users.browse'      => True,
        'users.search'      => True,
        'users.staff'       => True,
        );

// used for $current_forum = 'current';
$group3 = array(
        'forum'     => True,
        );

if(isset($group1[$p]))
    $current_home = 'current';
else if(isset($group2[$p]))
    $current_users = 'current';
else if(isset($group3[$p]))
    $current_forum = 'current';
else
    user_error("\$p is invalid", E_USER_ERROR);
Run Code Online (Sandbox Code Playgroud)

这看起来并不像a一样干净switch(),但它是唯一一个不包括编写一个小函数和类库以保持其整洁的快速解决方案.将项添加到数组中仍然非常容易.


too*_*php 25

版本2不起作用!!

case 'users.online' || 'users.location' || ...
Run Code Online (Sandbox Code Playgroud)

与以下内容完全相同:

case True:
Run Code Online (Sandbox Code Playgroud)

并且case将被选择为任何值$p,除非$p是空字符串.

||case声明中没有任何特殊含义,你不是要比较$p每个字符串,你只是检查它是否不是False.


kar*_*m79 8

将这些值放入数组并查询数组,因为switch-case似乎隐藏了当字符串变量用作条件时你想要实现的基本语义,使得它更难以阅读和理解,例如:

$current_home = null;
$current_users = null;
$current_forum = null;

$lotsOfStrings = array('users.online', 'users.location', 'users.featured', 'users.new');

if(empty($p)) {
    $current_home = 'current';
}

if(in_array($p,$lotsOfStrings)) {
    $current_users = 'current';
}

if(0 === strcmp('forum',$p)) {
    $current_forum = 'current';
}
Run Code Online (Sandbox Code Playgroud)


Pet*_*ter 6

为了完整起见,我将指出损坏的“第 2 版”逻辑可以替换为有效的 switch 语句,并且可以使用数组来提高速度和清晰度,如下所示:

// 用于 $current_home = 'current';
$home_group = 数组(
    '家' => 是的,
);

// 用于 $current_users = 'current';
$user_group = 数组(
    'users.online' => 真,
    'users.location' => 真,
    'users.featured' => 真,
    'users.new' => 真,
    'users.browse' => 真,
    'users.search' => 真,
    'users.staff' => 正确,
);

// 用于 $current_forum = 'current';
$forum_group = 数组(
    '论坛' => 是的,
);

开关(真){
    case isset($home_group[$p]):
        $current_home = '当前';
        休息;
    case isset($user_group[$p]):
        $current_users = '当前';
        休息;
    case isset($forum_group[$p]):
        $current_forum = '当前';
        休息;
    默认:
        user_error("\$p 无效", E_USER_ERROR);
}    


pgu*_*rio 5

其他一些尚未提及的想法:

switch(true){ 
  case in_array($p, array('home', '')): 
    $current_home = 'current'; break;

  case preg_match('/^users\.(online|location|featured|new|browse|search|staff)$/', $p):
    $current_users = 'current'; break;

  case 'forum' == $p:
    $current_forum = 'current'; break; 
}
Run Code Online (Sandbox Code Playgroud)

有人可能会抱怨 #2 的可读性问题,但我继承这样的代码没有问题。


lil*_*shu 5

现在你可以做...

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

这是演示该概念的一组现成的可运行代码:

<?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();
}
Run Code Online (Sandbox Code Playgroud)

以下(对于一次随机运行)输出如下:

 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
)
Run Code Online (Sandbox Code Playgroud)