phpjoomlavirtuemart

Multiple OR conditions not showing correctly


For some reason this "OR" statement is not working:

if($product->virtuemart_product_id != 153 || 170 || 171 || 195 || 208){
    //code here
}

It works when I only use one ID, but when I add many it doesn't work. Any ideas what could be wrong?


Solution

  • You are doing it wrong. The correct way is:

    if($product->virtuemart_product_id != 153 ||
       $product->virtuemart_product_id != 170 || 
       $product->virtuemart_product_id != 171 || 
       $product->virtuemart_product_id != 195 ||
       $product->virtuemart_product_id != 208)
    

    It looks messy, but this is the way to do it.

    A cleaner approach would be:

    $arr = [153,170,171,195,208];
    if( ! in_array($product->virtuemart_product_id, $arr) )
    

    Read more about in_array()