Php 如何改进if算法
如何做得更好?无需重复代码函数(1)Php 如何改进if算法,php,algorithm,Php,Algorithm,如何做得更好?无需重复代码函数(1) 让A=someTrueOrFalse和B=OthersomeTrueOrFalse A | B | outcome ----------------- 0 | 0 | 1 0 | 1 | 1 1 | 0 | 0 1 | 1 | 1 因此: if (!(someTrueOrFalse && !OthersomeTrueOrFalse)) { someFunction(1); } 或者,相当于@axi
让
A
=someTrueOrFalse和B
=OthersomeTrueOrFalse
A | B | outcome
-----------------
0 | 0 | 1
0 | 1 | 1
1 | 0 | 0
1 | 1 | 1
因此:
if (!(someTrueOrFalse && !OthersomeTrueOrFalse)) {
someFunction(1);
}
或者,相当于@axiac的评论
if (!someTrueOrFalse || OthersomeTrueOrFalse) {
someFunction(1);
}
这取决于两种情况中哪一种看起来更好,我猜(或者有时只是口味的问题)。这与
如果(!someTrueOrFalse | OthersomeTrueOrFalse)
这更容易理解。@axiac answer的方法看起来更容易理解:只有一个情境函数不能运行:第三个。所以,除了第三个以外的每个案例,都要运行它。您认为这3个案例是真实的,(1、2和4),然后您描述它们。@axiac谢谢,在回答中补充了这一点,尽管我的回答只是想说明如何使用真值表实现这一点。
if (!someTrueOrFalse || OthersomeTrueOrFalse) {
someFunction(1);
}