在我的模板条件中使用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>}
?>
我有更多彩色产品(变量值)。并希望使用以上语句为黑色,蓝色,红色,紫色,绿色设置条件。是否可以使用两种不同的颜色值数组作为条件?
答案 0 :(得分: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>
}
?>