如何檢查三個變量中只有一個不為空?

[英]How can I check whether only one out of three variables is not empty?


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.

變量都包含文本。

8 个解决方案

#1


33  

You can convert variable into array and exclude empty variables using array_filter(). Then use count() after the filter.

您可以使用array_filter()將變量轉換為數組並排除空變量。然后在過濾器后使用count()。

if(count(array_filter(array($var1,$var2,$var3)))==1){
  //only 1 variable is not empty
}

Check Fiddle link

檢查小提琴鏈接

#2


25  

Booleans return 0 and 1 with array_sum()

布爾值使用array_sum()返回0和1

if (array_sum(array(empty($var1), empty($var2), empty($var3))) == 1)
{
    echo "one is empty" ;
}

ETA: This is a simpler way:

ETA:這是一種更簡單的方法:

if (!empty($var1) + !empty($var2) + !empty($var3) == 1) {
    echo "1 is not empty" ;
}

ETA 2: We don't need the negative signs

ETA 2:我們不需要負號

if (empty($var1) + empty($var2) + empty($var3) == 2) {
    echo "1 is not empty" ;
}

#3


10  

$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

小提琴

Or you can simply do

或者你可以干脆做

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

Fiddle

小提琴

#4


7  

I'd use XOR (exclusive or) for this, because it's intended for this purpose, so using a dirty workaround with an array is not as easy to understand.

我會為此使用XOR(獨占或),因為它是為此目的而設計的,因此使用數組的臟解決方法並不容易理解。

if (!(!empty($var1) && !empty($var2) && !empty($var3)) && (!empty($var1) ^ !empty($var2) ^ !empty($var3))) {
    echo "Only one string is not empty\n";
}

And it's about 25% faster than the accepted answer.

它比接受的答案快25%左右。

$before = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
    $var1 = 'Hello';
    $var2 = '';
    $var3 = '';

    if (!(!empty($var1) && !empty($var2) && !empty($var3)) && (!empty($var1) ^ !empty($var2) ^ !empty($var3))) {
        echo "Only one string is not empty\n";
    }

    $var4 = '';
    $var5 = '';
    $var6 = '';

    if (!(!empty($var4) && !empty($var5) && !empty($var6)) && (!empty($var4) ^ !empty($var5) ^ !empty($var6))) {
        echo "Only one string is not empty\n";
    }

    $var7 = 'Hello';
    $var8 = 'World';
    $var9 = '!';

    if (!(!empty($var7) && !empty($var8) && !empty($var9)) && (!empty($var7) ^ !empty($var8) ^ !empty($var9))) {
        echo "Only one string is not empty\n";
    }
}

$after = microtime(true);
echo ($after-$before)/$i . " sec for XOR\n";

// 3.2943892478943E-6 sec for XOR

$before = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
    $var1 = 'Hello';
    $var2 = '';
    $var3 = '';

    if (count(array_filter(array($var1, $var2, $var3))) == 1) {
        echo "Only one string is not empty\n";
    }

    $var4 = '';
    $var5 = '';
    $var6 = '';

    if (count(array_filter(array($var4, $var5, $var6))) == 1) {
        echo "Only one string is not empty\n";
    }

    $var7 = 'Hello';
    $var8 = 'World';
    $var9 = '';

    if (count(array_filter(array($var7, $var8, $var9))) == 1) {
        echo "Only one string is not empty\n";
    }
}
$after = microtime(true);
echo ($after-$before)/$i . " sec for Arrays\n";

// 4.3078589439392E-6 sec for Arrays

*I had to update the answer because the name "exclusive or" is somewhat misleading in context of more than two expressions. Of course all commenters are right, and exclusive or is a binary operation therefore resolving from left to right. 1 ^ 1 ^ 1 == 1 resolves to 0 ^ 1 == 1 and is therefore true. Exclusive or does actually look for an odd number of trues.

*我必須更新答案,因為名稱“exclusive or”在兩個以上表達式的上下文中有些誤導。當然,所有評論者都是正確的,並且是排他性的或者是二元操作,因此從左到右解析。 1 ^ 1 ^ 1 == 1解析為0 ^ 1 == 1因此為真。獨家或實際上尋找奇數的真實。

I updated my answer with an easy-to-read workaround, but this definitely doesn't satisfy me and I have to admin that I resolved a huge misconception of boolean operators in my mind. The last time was a wrong assumption of AND and OR being resolved from left to right rather than first AND then OR.*

我用一個易於閱讀的解決方法更新了我的答案,但這絕對不能滿足我,我必須管理員,我在腦海中解決了對布爾運算符的巨大誤解。最后一次是錯誤的AND假設,OR從左到右解決,而不是先解決,然后是OR。*

#5


4  

Try this one:

試試這個:

if (($var1 !== '' && $var2 == '' && $var3 == '') ||
    ($var2 !== '' && $var1 == '' && $var3 == '')  ||
    ($var3 !== '' && $var1 == '' && $var2 == '')) {
    echo 'variable is empty';
}

#6


3  

Bitwise XOR is great for this:

Bitwise XOR非常適用於此:

$var1 ^ $var2 ^ $var3

You might have trouble if the variables don't cast to boolean easily, in which case you'd need to do empty($var) on each of them.

如果變量不容易轉換為布爾值,則可能會遇到麻煩,在這種情況下,您需要對每個變量執行空($ var)。

Boom. Zero ifs.

繁榮。零ifs。

Update

更新

Oops, if they are all not empty, true ^ true ^ true == true

哎呀,如果它們都不是空的,那么真的是^ true ^ = = true

You'll need to check against all of them being true:

你需要檢查所有這些都是真的:

($var1 ^ $var2 ^ $var3) && !($var1 && $var2 && $var3)

#7


2  

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!

這是您應該使用數組的情況。你現在只有3個值,但如果你需要4個怎么辦?您需要更改所有代碼!

$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. The main principle of this code is that if you add more values, the logic itself doesn't need changing.

如果你確實需要chek零或空字符串,你可以使用其他方法來計算。此代碼的主要原則是,如果添加更多值,邏輯本身不需要更改。

#8


0  

Try this:

嘗試這個:

$temp_array = array($var1,$var2,$var3);
$temp_count = count(array_filter($temp_array, 'strlen'));
if($temp_count ==1){
    echo "1 variable is not empty";
}
else
{
    echo "total empty variable  is = ".$temp_count;
}

DEMO

DEMO


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/09/14/724e5efce5853bd1594f5114f12e48b1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com