How to declare various conditions within an if

6

I have two ifs that take into account 3 conditions, one of them with multiple possibilities.

I'm doing this, but it's not working:

if (($var1 == $var2) && ($var3 == 1 || 3 || 5 || 7 || 8 || 12) && ($var4 > 16)) {
    $var5--;
}

if (($var1 == $var2) && ($var3 == 4 || 6 || 9 || 11) && ($var4 > 15)) {
    $var5--;
}

In my tests the script is always taking 2 of $var5 , when it was to remove only 1, because $var3 will never match in both cases. So I guess he's not considering this condition. I've already researched here, and all the examples I've found show at most two conditions.

What is the correct way to declare various conditions within a if , some of these conditions may have multiple possibilities and use other operators within the condition ( $var3 in the above example). Thanks in advance.

    
asked by anonymous 06.06.2015 / 02:11

1 answer

9

As qmechanik said , it would need to be

($var3 == 1 || $var3 == 3 || $var3 == 5 || $var3 == 7 || $var3 == 8 || $var3 == 12)

That's because the way you did, 1 || 3 || 5 || 7 || 8 || 12 is understood as a single expression ( $var3 == (1 || 3 || 5 || 7 || 8 || 12) ), which results in 1 , which is true .

Another way to do this is to use in_array , get a little cleaner:

$opcoesVar3 = array(1, 3, 5, 7, 8, 12);
if (($var1 == $var2) && in_array($var3, $opcoesVar3) && ($var4 > 16)) {
    $var5--;
}
    
06.06.2015 / 02:38