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
#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 if
s.
繁荣。零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
#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";
小提琴
Or you can simply do
或者你可以干脆做
var_dump(count(array_filter(array($var1,$var2,$var3)))==1);
小提琴
#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 if
s.
繁荣。零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