I am trying to do a conditional if statement that checks whether the value of a variable $cat_ID is not equal to 19 or 26 then it should echo my $priceToShow variable.
PHP
if(($cat_id != '19') || ($cat_id !='26')){
echo $priceToShow;
}
I am trying to do a conditional if statement that checks whether the value of a variable $cat_ID is not equal to 19 or 26 then it should echo my $priceToShow variable.
PHP
if(($cat_id != '19') || ($cat_id !='26')){
echo $priceToShow;
}
If it can be neither 19 nor 26, use an and statement:
if(($cat_ID != '19') && ($cat_id !='26')){
echo $priceToShow;
}
If you have a lot of values to check, use in_array
:
$bad_values = array(19, 26, 54);
if (!in_array($cat_ID, $bad_values)) {
echo $priceToShow;
}
(In this case, strict comparisons are off; you should always cast your data to the type it's expected to be, and then use strict comparison:
$bad_values = array(19, 26, 54);
if (!in_array(intval($cat_ID), $bad_values, true)) {
echo $priceToShow;
}
)
This will always return true
You need to use an AND conjunction
of for more than 2 values use ! in_array()
Edit: Absolutely right @Waygood
if ( ! ( $v == 19 || $v == 26 ) ) {
// do your thing
}
It might be better to use !in_array()
. That'll make it quicker and simpler to add and remove when/if needed
if (!in_array($cat_id, array('19', '26')))
{
echo $priceToShow;
}