所以我有一个变量命名bestmatchrule返回1-25现在林间试图分配一个号码的名称使用下面的if语句单号:如何压缩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="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";
}
问题是,它不工作。它只返回最上面的一个。这是返回值还是if语句的问题?
此外,如果有缩短它的方法,使其看起来更好?我更关注功能,但必须有比这更好的方法吗?
在此先感谢! (p。对不起noob问题!)
在猜测你可能需要更接近的东西:'if($ a ==“1”|| $ a ==“2”)...'至于更好的方法,显而易见的将是一个数组。 –
将此数组转换为数组可能会更加容易 –
array'$ x = array(1 =>'Chesil House',2 =>'Chesil House',3 =>'Corfe House')...' –