PHP - Check if more than one condition is true in a given number of conditions

Is there an elegant way to check if multiple, but not all, conditions are true from any given number of conditions?

For example, I have three variables: $ a, $ b and $ c. I want to check that all two of them are correct. So the following:

$a = true; $b = false; $c = true; 

But this is not so:

 $a = false; $b = false; $c = true; 

In addition, I can check if 4 out of 7 conditions were true.

I understand that I can check each combination, but this will become more complicated as the number of conditions increases. Penetrating through conditions and keeping the count is the best option I can think of, but I thought there might be another way to do this.

Thanks!

Edit: Thanks for all the great answers, they are very much appreciated. Just to put a wrench on the job, what if the variables were not explicit logical? For instance.

 ($a == 2) ($b != "cheese") ($c !== false) ($d instanceof SomeClass) 
+5
source share
6 answers

"true" boolean in PHP adds to 1 as an integer, and "false" - 0. Therefore:

 echo $a + $b +$c; 

... will output 2 if two of the three boolean variables $a , $b or $c are true. (Adding values ​​will implicitly convert them to integers.)

This will also work with functions like array_sum() , for example:

 echo array_sum([true == false, 'cheese' == 'cheese', 5 == 5, 'moon' == 'green cheese']); 

... outputs 2.

+8
source

You can put your variables in an array and use array_filter() and count() to check the number of true values:

 $a = true; $b = false; $c = true; if (count(array_filter(array($a, $b, $c))) == 2) { echo "Success"; }; 
+4
source

I would use a method similar to the following:

 if (evaluate(a, b, c)) { do stuff; } boolean evaluate(boolean a, boolean b, boolean c) { return a ? (b || c) : (b && c); } 

What he says:

  • If a is true, then one of b or c must be true in order to meet 2/3 True criteria.
  • Otherwise, both b and c must be true!

If you want to expand and customize the conditions and number of variables for which I would like to find a solution, for example the following:

 $a = true; $b = true; $c = true; $d = false; $e = false; $f = true; $condition = 4/7; $bools = array($a, $b, $c, $d, $e, $f); $eval = count(array_filter($bools)) / sizeof($bools); print_r($eval / $condition >= 1 ? true : false); 

We just evaluate the truth, and we make sure that% of True is equal to or better than what we want to achieve. Similarly, you can manipulate the final evaluation expression to achieve what you want.

+1
source

This should also work and allow you to set the number fairly easily.

 $a = array('soap','soap'); $b = array('cake','sponge'); $c = array(true,true); $d = array(5,5); $e = false; $f = array(true,true); $g = array(false,true); $pass = 4; $ar = array($a,$b,$c,$d,$e,$f,$g); var_dump(trueornot($ar,$pass)); function trueornot($number,$pass = 2){ $store = array(); foreach($number as $test){ if(is_array($test)){ if($test[0] === $test[1]){ $store[] = 1; } }else{ if(!empty($test)){ $store[] = 1; } } if(count($store) >= $pass){ return TRUE; } } return false; } 
+1
source

I think this is a little light and short letter when you use the operator "&"., "|" eg:

 $a = true; $b = true; $c = false; $isTrue = $a&$b | $b&$c | $c&$a; print_r( $isTrue ); 

Check for yourself: D

0
source

U can use a while loop:

 $condition_n = "x number"; // number of required true conditions $conditions = "x number"; // number of conditions $loop = "1"; $condition = "0"; while($loop <= $conditions) { // check if condition is true // if condition is true : $condition = $condition + 1; // $loop = $loop + 1; } if($condition >= $condition_n) { // conditions is True } else { // conditions is false } 
0
source

Source: https://habr.com/ru/post/1258128/


All Articles