Question: I've got four variables, and if any two or more of them return true, I need to throw an error. I could technically do something like this:

`if (($var1 && $var 2) || ($var1 && $var3) || ($var1 && $var 4) ... || ($var4 && $var3))`, but that gets super complex and hard to manage. I imagine there's got to be a better way to do this.

Thanks.

Answer: 
Edit: `array_filter` with only the array passed will return an array with whatever values in the given array juggle to `true` still in it. So the simplest and most foolproof way to implement what I described in earlier edits is probably just

```
if (count(array_filter([$a, $b, $c, $d])) >= 2) {
    // error
}

```

---

Original post:

With [type juggling](http://php.net/manual/en/language.types.type-juggling.php), you can essentially treat boolean values as integers. `true` is `1`, and `false`, `0`. Given four variables that are booleans, you can just add them up\*:

```
if (array_sum([$a, $b, $c, $d]) >= 2) {
    throw new WhateverException;
}

```

If the values really aren't boolean, you can cast them explicitly:

```
if (array_sum(array_map(function($i) { return (bool) $i; }, [$a, $b, $c, $d])) >= 2) {
    // error
}

```

\*I use `array_sum` for maximum flexibility because I assume that the number of values may change. If it really won't\*\*, then `$a+$b+$c+$d` is fine.

\*\*It will.
Beste way is to use an array and loop through the array.

```
$my_vars = array($var1, $var2, $var3, ...);
$test_var = 'something';
$hits = 0;
foreach($my_vars as $var) {
    $hits = $var == $test_var ? $hits++: $hits;
    if($hits >= 2) {
        throw new Exception('This is bad dude...);
    }
}

```