PHP combining boolean operators (AND, OR) in an if statement

boolean, if-statement, php

Solution

You should use inner parentheses for precedence ..

The reason is.. the first condition that was wrapped inside the parentheses will be evaluated as a block

As the other user mentioned, `&&` has higher precedence over the `||` there is no need for the parentheses , but say if your `if` statement goes on like this..

if($apple==1 || $orange==2 && cake==0) 

Then you need to go on with ..

if(($apple==1 || $orange==2) && cake==0)

Sidenote : Always its a good practice to use parantheses...

Problem

i wonder if its possible to combine both operators (OR and AND) in one if statement like this. ``` if($apple==1 && $orange==2 || cake==0) ``` What i want to state is: if apple equals 1 AND orange equals 2, OR cake equals 0 then do this. In other words, i need apple and orange to equal the numbers stated above OR cake to equal 0. Is this expression correct? If not, whats the simplest way to do it? Thank you.

Original source