一个if($ a == $ b || $ a == $ c)语句的速记表达式 [英] Shorthand expression for an if ( $a == $b || $a == $c ) statement
本文介绍了一个if($ a == $ b || $ a == $ c)语句的速记表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这段代码可以工作:
I know this code will work:
echo ( $a == $b || $a == $c ) ? "Yes" : "No";
可以这样读取:
如果$ a等于$ b或$ a等于$ c
if $a is equal to $b or $a is equal to $c
有没有办法使其更短,如:
Is there a way to make it more shorter like:
如果$ a等于$ b或$ c
if $a is equal to $b or $c
我已经尝试了很多,但还没有运气:
I have tried a lot including this but still no luck:
echo ( $a == ( $b xor $c ) ) ? "Yes" : "No";
推荐答案
您可以使用 in_array
:
You can use in_array
:
var_dump(in_array($a, [$b, $c]));
与您的示例:
echo in_array($a, [$b, $c]) ? 'Yes' : 'No';
注意:此语法仅在有超过2个值时有用。对于几个值 $ a == $ b || $ a == $ c
做得很好,可能更快。
Note: this syntax is only useful if you have more than 2 values. For few values $a == $b || $a == $c
does the job well and is probably faster.
这篇关于一个if($ a == $ b || $ a == $ c)语句的速记表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文