lundi 24 octobre 2016

How to test if multiple (2 or more) booleans are true

I have a bunch of boolean variables. Some of the variables could be TRUE and others could be FALSE. For this example i'll have Ten (10) variables:

//Variables
$a $b $c $d $e $f $g $h $i $j;

The goal is to test and execute code when two or more variables are true. If I declare a condition for each possible variable combination, the if statement will become long and unmaintainable.

//Condition Example
if ( ($a && $b) ||
     ($a && $c) ||
     ($a && $d) ||
     ($a && $e) ||
     ($a && $f) ||
     ($a && $g) ||
     ($a && $h) ||
     ($a && $i) ||
     ($a && $j) ||
     ($b && $c) ||
     ($b && $d) ||
     ($b && $e) ||
     ($b && $f) ||
     ($b && $g) ||
     (etc.) ){
        //DO something
}

How would I test to see if 2 or more of the variables are TRUE?

Aucun commentaire:

Enregistrer un commentaire