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!)

图片转代码服务由CSDN问答提供 功能建议

所以我有一个名为bestmatchrule的变量,它返回1-25之间的单个数字,现在我试图分配该数字 使用以下if语句的名称:

  $ 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 =”学生村“; 
} 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 Tr  y再次“; 
} 
   
 
 

问题是它不起作用。 它只返回前一个。 这是返回值或if语句的问题吗?

另外,如果有缩短它的方法使它看起来更好吗? 我更专注于功能,但必须有比这更好的方法吗?

提前致谢! (对不起这个菜鸟问题抱歉!) \ n

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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条)

相关推荐 更多相似问题