2011-08-09 78 views
4

有沒有更好的方法來重新定義這if(),我不喜歡這個說法是$prefix重複一遍又一遍,它看起來很醜。有沒有更好的方法來定義if()條件使用PHP?

if($prefix == 'RSVH' || 
    $prefix == 'RSAP' || 
    $prefix == 'CMOS' || 
    $prefix == 'CMSR' || 
    $prefix == 'CMKS' || 
    $prefix == 'CMWH' || 
    $prefix == 'CMBL' || 
    $prefix == 'LNRS' || 
    $prefix == 'LNCM' || 
    $prefix == 'LNMX' || 
    $prefix == 'PMNG'); 

謝謝你..

+0

你也可以使用開關/箱但下面的建議更好,假設你把前綴放在一個數組中 – Will

回答

7

你可以使用一個陣列和功能in_array()

$values = array('RSVH', 'RSAP', 'CMOS' /*, ... */); 

if (in_array($prefix, $values)) 
{ 
    /* do something */ 
} 
6

也許我會用in_array()

if (in_array($prefix, array('RSVH','RSAP','CMOS'.....)) 
{ 
    // It's in there... 
} 
+0

ye啊,這看起來更好,把它放在一個數組中,並檢查它是否存在於一個數組中:) –

相關問題