1
votes

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
4

4 Answers

1
votes

You should create an array to contain your variables. Then iterate through them.

Eg:

$a = true;
$b = false;
$c = true;
$d = false;
$e = false;
$booleanVariables = [
    $a,
    $b,
    $c,
    $d,
    $e
];

$count = 0;
foreach ($booleanVariables  as $variable) {
    if ($variable) {
        $count++;
        // Do something here
    }
}
echo $count;

DEMO

1
votes

The easiest way to do this (IMO) is to put them into an array, then loop over it, shortcutting the loop if you find two true values. You can put it in a function like so:

<?php
// put vars into array
$varArray = array(
    $a, $b, $c, $d, $e, $f, $g, $h, $i, $j
);

if(twoTrue($varArray)){
    // Do something
}



function twoTrue($arrayOfVars){
    $trueCount = 0;
    foreach($arrayOfVars as $aVar){
        if($aVar)
            $trueCount++;

        if($trueCount >= 2)
            return true;
    }
    return false;

}

Worst case scenario would be having to iterate through the loop N times, once for each variable.

1
votes

I like the array approach, however I would filter out false values and count if there are more than 1:

if(count(array_filter([$a,$b,$c,$d,$e,$f,$g,$h,$i,$j])) > 1) {
    // yes
}

Actually on second thought, true will evaluate to 1 and false to 0.

I personally would use this one:

if(array_sum([$a,$b,$c,$d,$e,$f,$g,$h,$i,$j]) > 1) {
    // yes
}

Things just keep popping into my head. For one true it will be 1, for two it will be 11, for three 111 etc...

if(implode([$a,$b,$c,$d,$e,$f,$g,$h,$i,$j]) > 1) {
    // yes
}

If your PHP version doesn't support [] for arrays just use array().

0
votes

You can put a list of boolean variables and loop the list with while and check if you stop when you have 2 true. i wish that help you.