douxieshang5577 2015-06-09 01:10
浏览 29
已采纳

如何压缩if语句代码?

So I have a variable named bestmatchrule which returns a single number between 1-25 and now Im trying to assign that number a name using the following if statement:

$a=$bestmatchrule;
$b='';

if ($a == "1" || "2") {
      $b="Chesil House";
} elseif ($a == "3" || "4") {
      $b="Corfe House";
} elseif ($a == "5" || "6") {
      $b="Cranborne House";
} elseif ($a == "7" || "8") {
        $b="Dorchester House";
} elseif ($a == "9" || "10") {
      $b="Lyme Regis House";
} elseif ($a == "11" || "12") {
      $b="Okeford House";
} elseif ($a == "13" || "14") {
      $b="Purbeck House";
} elseif ($a == "15" || "16") {
      $b="Student Village";
} elseif ($a == "17" || "18" || "19") {
      $b="Unilet";
} elseif ($a == "20") {
      $b="Conel Court";
} elseif ($a == "21" || "22") {
      $b="St John's Road";
} elseif ($a == "23" || "24" || "25") {
      $b="Private Let";
} else {
     "Please Try Again";
}

Problem is it doesn't work. It only returns the top one. Is this an issue with the value returned or the if statement?

Also, if there a way of shortening it to make it look nicer? I'm more focused on functionality, but there has to be a better way than this?

Thanks in advance! (p.s. sorry for the noob question!)

  • 写回答

2条回答 默认 最新

  • drygauost253590142 2015-06-09 01:13
    关注

    $a == "1" || "2" is not a valid statement. It would need to be $a == "1" || "$a == 2". That's your error.

    As far as shortening this, there isn't much you can do. You can use in_array() to shorten some statements but you don't really have any where that would be truly handy.

    if ($a == "1" || $a == "2") {
          $b="Chesil House";
    } elseif ($a == "3" || $a == "4") {
          $b="Corfe House";
    } elseif ($a == "5" || $a == "6") {
          $b="Cranborne House";
    } elseif ($a == "7" || $a == "8") {
            $b="Dorchester House";
    } elseif ($a == "9" || $a == "10") {
          $b="Lyme Regis House";
    } elseif ($a == "11" || $a == "12") {
          $b="Okeford House";
    } elseif ($a == "13" || $a == "14") {
          $b="Purbeck House";
    } elseif ($a == "15" || $a == "16") {
          $b="Student Village";
    } elseif (in_array($a, array("17", "18","19")) {
          $b="Unilet";
    } elseif ($a == "20") {
          $b="Conel Court";
    } elseif ($a == "21" || $a == "22") {
          $b="St John's Road";
    } elseif (in_array($a, array("23", "24", "25")) {
          $b="Private Let";
    } else {
         echo "Please Try Again";
    }
    

    You can also use an array to hold your values wit the $a value being the key. Then just check to see if it is in the array and if so, assign the value:

    $array = [
        1 => "Chesil House",
        2 => "Chesil House",
        3 => "Corfe House",
        4 => "Corfe House",
        5 => "Cranborne House",
        6 => "Cranborne House",
        7 => "Dorchester House",
        8 => "Dorchester House",
        9 => "Lyme Regis House",
        10 => "Lyme Regis House",
        11 => "Okeford House",
        12 => "Okeford House",
        13 => "Purbeck House",
        14 => "Purbeck House",
        15 => "Student Village",
        16 => "Student Village",
        17 => "Unilet",
        18 => "Unilet",
        19 => "Unilet",
        20 => "Conel Court",
        21 => "St John's Road",
        22 => "St John's Road",
        23 => "Private Let",
        24 => "Private Let",
        25 => "Private Let"
    ];
    
    if (isset($array[$a])) {
        $b = $array[$a];
    }
    else {
        echo "Please Try Again";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。