2015-06-09 95 views
2

所以我有一個變量命名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問題!)

+2

在猜測你可能需要更接近的東西:'if($ a ==「1」|| $ a ==「2」)...'至於更好的方法,顯而易見的將是一個數組。 –

+0

將此數組轉換爲數組可能會更加容易 –

+0

array'$ x = array(1 =>'Chesil House',2 =>'Chesil House',3 =>'Corfe House')...' –

回答

4

$a == "1" || "2"是不是一個有效的聲明。它需要是$a == "1" || "$a == 2"。這是你的錯誤。

至於縮短這一點,你可以做的不多。您可以使用in_array()縮短一些陳述,但您並沒有真正有用的地方。

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"; 
} 

你也可以使用一個數組來保存你的值,其中$a值是關鍵。然後只是檢查它是否在陣列中,如果是,分配值:

$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"; 
} 
+0

是英雄! 很多很多感謝你! – user3122952

1

John Conde的答案可能是你想要的。另一種可能性是漸進式的(<)報表。

$a= (int) $bestmatchrule; 
$b=''; 

if ($a < 3) { 
     $b="Chesil House"; 
} elseif ($a < 5) { 
     $b="Corfe House"; 
} elseif ($a < 7) { 
     $b="Cranborne House"; 
} elseif ($a < 9) { 
     $b="Dorchester House"; 
} elseif ($a < 11) { 
     $b="Lyme Regis House"; 
} elseif ($a < 13) { 
     $b="Okeford House"; 
} elseif ($a < 15) { 
     $b="Purbeck House"; 
} elseif ($a < 17) { 
     $b="Student Village"; 
} elseif ($a < 20) { 
     $b="Unilet"; 
} elseif ($a < 21) { 
     $b="Conel Court"; 
} elseif ($a < 23) { 
     $b="St John's Road"; 
} elseif ($a < 26) { 
     $b="Private Let"; 
} else { 
    "Please Try Again"; 
} 

switch語句也是一種選擇,但我不認爲這會更好。