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?
EDIT/ADDITION:
Thanks for your answers. using PHP's array_filter() appears to perform the best in varying circumstances. Out of curiosity I tested the performance of both @AbraCadaver and @Chizzle answers.
Low values (array value 5 and 10 of 1000 are true)
count(array_filter($array)) : Execution time = 0.00319
array_sum($array) : Execution time = 0.00369
implode($array) : Execution time = 0.02098
foreach($array) : Execution time = 0.00939
Mid values (array value 400 and 500 of 1000 are true)
count(array_filter($array)) : Execution time = 0.00247
array_sum($array) : Execution time = 0.00340
implode($array) : Execution time = 0.01091
foreach($array) : Execution time = 1.00006
High values (array value 950 and 1000 of 1000 are true)
count(array_filter($array)) : Execution time = 0.00250
array_sum($array) : Execution time = 0.00329
implode($array) : Execution time = 0.01089
foreach($array) : Execution time = 1.02739