Abhik
Abhik

Reputation: 674

Conditional If Conditions... Possible?

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

Answers (6)

Eyesis
Eyesis

Reputation: 191

if ($clauseA) { if ($clauseB) { B } else { A } // Do stuff }

Upvotes: 0

DaKirsche
DaKirsche

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

Maxim Kumpan
Maxim Kumpan

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

Steven Moseley
Steven Moseley

Reputation: 16325

Looks like you're trying to do this....

  1. If Clause A is true, evaluate A as part of your condition, OR
  2. If Clause B is true, evaluate B as part of your condition:

Code:

if (($clauseA && A) || ($clauseB && B)) {
    // Do Stuff
}

Upvotes: 2

Leonard
Leonard

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

deceze
deceze

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

Related Questions