0

In my template condition are evaluated using php if else statement as follows

<?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>}
?>

I have more colored products (variable values ). and want to set condition using above statement for black,blue,red,purple,green. Is it possible using two different array of color value as condition?

galexy
  • 343
  • 3
  • 16
  • 37
  • 1
    *Warning: `strpos` may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Use the `===` operator for testing the return value of this function.* – deceze Mar 27 '14 at 13:05

1 Answers1

1

Please replace your code with below code:

<?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>
}

?>
Nikunj Kabariya
  • 840
  • 5
  • 14