[英]Conditional If Conditions… Possible?
Sorry, I am pretty new to php. 对不起,我对php很新。
Is something like this possible? 这样的事情可能吗?
if ( if ($clauseA) { A } if ($clauseB) { || B } ) {
// Do Stuff
}
What I want to do is if $clauseA is true, include A in condition and if $clauseB is true, then include || 我想要做的是如果$ clauseA为真,包括A条件,如果$ clauseB为真,则包括|| B (OR and B) in condition.
B(OR和B)处于状态。
Is there any solid way to achieve this? 有没有可靠的方法来实现这一目标?
Looks like you're trying to do this.... 看起来你正试图这样做....
Code: 码:
if (($clauseA && A) || ($clauseB && B)) {
// Do Stuff
}
Logical or
, aliased ||
逻辑
or
别名||
, always works this way. ,总是以这种方式工作。 If the first conditional expression does not evaluate to true, the second conditional expression is not tested or executed.
如果第一个条件表达式的计算结果为true,则不测试或执行第二个条件表达式。
The same goes for and
( &&
), it will not continue if the first expression evaluates to false. 对于
and
( &&
)也是如此,如果第一个表达式的计算结果为false,它将不会继续。
Update: In fact, I think I just understood the question. 更新:事实上,我认为我只是理解了这个问题。 :)
:)
if ( ($clauseA && A) || ($clauseB && B) ) {
// A and B will only be checked if the appropriate clause is true, otherwise they are ignored.
}
if ($clauseA) { if ($clauseB) { B } else { A } // Do stuff }
In boolean logic, this is expressed as: 在布尔逻辑中,这表示为:
if (($clauseA && $a) || ($clauseB && $b))
Or some variation thereof, depending on what exactly you want to do. 或者其中的一些变化,取决于您想要做什么。 The point being, you can express it using a combination of
&&
and ||
关键是,您可以使用
&&
和||
的组合来表达它 , without if
. ,
if
没有。
If A and B are boolean conditions that only have to be true if $clauseA or $clauseB are true as well, just rewrite your condition like this: 如果A和B是布尔条件,如果$ clauseA或$ clauseB也为真,则只需要为true,只需重写您的条件,如下所示:
if( (!$ClauseA || $A) && (!$ClauseB || $B)) {
// Do Stuff
}
I just applied De Morgan's laws ( http://en.wikipedia.org/wiki/De_Morgan 's_laws) to accomplish this. 我刚刚应用了De Morgan的法律( http://en.wikipedia.org/wiki/De_Morgan's_laws )来实现这一目标。
Depending on the following: $clauseA might be true and/or $clauseB may be true, but both can be false: 取决于以下内容:$ clauseA可能为true和/或$ clauseB可能为true,但两者都可能为false:
if ((($clauseA || $clauseB) && A) || ($clauseB && B))
{
// Doing some stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.