2014-03-27 28 views
0

在我的模板條件使用PHP的if else語句如下包括多個字符串作爲條件在PHP的if else語句

<?php 
$my_product_color = 'black shirt' ///it may be blue jeans,red shoes ,yellow belt etc 


if ((strpos($my_product_color ,'black') == false)){ 
//content 
<h3>You are eligible for discounted rate for all light colors<h3> 
} else { 

//another content 
<h3>no discount on dark colors <h3>} 
?> 

我有更多的彩色產品(變量值)進行評估。並希望使用上面的語句爲黑色,藍色,紅色,紫色,綠色設置條件。是否有可能使用兩種不同的顏色值作爲條件?

+1

*警告:'strpos'可能返回布爾值FALSE,但也可能返回的結果爲FALSE的非布爾值。使用'==='運算符來測試這個函數的返回值。* – deceze

回答

1

請在下面的代碼替換代碼:

<?php 

// make an array for fixed colors 
$color_array = array('black','blue','red','purple','green'); 

// now explode by space as you said : it may be blue jeans,red shoes ,yellow belt etc 
$my_product_color = 'black shirt'; 
$exploded_product_color = explode('',$my_product_color); 

if(in_array($exploded_product_color[0],$color_array)) 
{ 
    //content 
    <h3>You are eligible for discounted rate for all light colors<h3> 
} 
else 
{ 
    //another content 
    <h3>no discount on dark colors <h3> 
} 

?>