Reputation: 674
Sorry, I am pretty new to 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 || B (OR and B) in condition.
Is there any solid way to achieve this?
Upvotes: 0
Views: 107
Reputation: 352
Depending on the following: $clauseA might be true and/or $clauseB may be true, but both can be false:
if ((($clauseA || $clauseB) && A) || ($clauseB && B))
{
// Doing some stuff
}
Upvotes: 0
Reputation: 2625
Logical or
, aliased ||
, always works this way. If the first conditional expression does not evaluate to true, the second conditional expression is not tested or executed.
The same goes for and
(&&
), it will not continue if the first expression evaluates to 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.
}
Upvotes: 0
Reputation: 16325
Looks like you're trying to do this....
Code:
if (($clauseA && A) || ($clauseB && B)) {
// Do Stuff
}
Upvotes: 2
Reputation: 458
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:
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.
Upvotes: 0
Reputation: 522085
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
.
Upvotes: 0