superUntitled
superUntitled

Reputation: 22547

PHP "if()" construct: can conditionals nest?

Is it possible to do something like this in a php if statement:

if($a == '1' || ($b == '2' && $c == '3')) echo "foo walks into a bar";

(also, is the title of my question phrased correctly?)

Upvotes: 2

Views: 150

Answers (5)

Alan Whitelaw
Alan Whitelaw

Reputation: 16900

Yes

php.net/if

Upvotes: 2

KingCrunch
KingCrunch

Reputation: 132021

Of course you can.

http://www.php.net/manual/en/language.operators.precedence.php

Upvotes: 4

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

They absolutely can! I think it'd be called something like a "multiple condition if()" statement.

Upvotes: 0

user193476
user193476

Reputation:

Yes, as $a == '1' || ($b == '2' && $c == '3') evaluates into a Boolean expression. Think of || and && as mathematical operators, and you can apply brackets to them to alter their order of operations.

Upvotes: 5

James Love
James Love

Reputation: 1020

Think you might need to bracket up the first condition:

if(($a == '1') || ($b == '2' && $c == '3')) echo "foo walks into a bar";

Upvotes: 0

Related Questions