Question: I have three variables:

```
$var1
$var2
$var3

```

I'm actually looking for the best way to check if **only one** of these three variables is not empty and the **two others** are empty.

Is that possible to do this with one `if` only? If not, then what's the best way?

The variables all contain text.

Answer: 
```
$counter=0;
$counter+= empty($var1) ? 0:1;
$counter+= empty($var2) ? 0:1;
$counter+= empty($var3) ? 0:1;

if($counter==1)
   echo "Exactly 2 are empty";

```

**[Fiddle](https://eval.in/641962)**

Or you can simply do

```
var_dump(count(array_filter(array($var1,$var2,$var3)))==1);

```

**[Fiddle](https://eval.in/641979)**
This is a situation where you should use arrays. You now only have 3 values, but what if you need 4? You'll need to change all your code!

```
$var = array();
$var[] = 'abc';
$var[] = '';
$var[] = 0;

// will return 1, empty values, false or 0 (falsy values) will not get counted:
echo count(array_filter($var)).' values found';
if( count(array_filter($var))==1 ){ echo 'exactly one value set'; }

```

If you do need to chek zero's or empty strings [you can use other methods to count](https://stackoverflow.com/questions/4422889/how-to-count-non-empty-entries-in-php-array). The main principle of this code is that if you add more values, the logic itself doesn't need changing.