antispin Posted September 16, 2009 Share Posted September 16, 2009 if (5 or 0 || (5/0)) { echo "big"; } else { echo "small"; } This code should return a division by zero, because the "||" should be evaluated before "or". Why doesn't it? /confused Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/ Share on other sites More sharing options...
ToonMariner Posted September 16, 2009 Share Posted September 16, 2009 ermmm I think you are missing something there fella. the conditional will evaluate to a true or flase. your statments is saying... if 5 OR 0 OR (5/0 [which will give a warning]) is true... this conditional will ALWAYS return true so you will always get big echoed out Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919452 Share on other sites More sharing options...
antispin Posted September 16, 2009 Author Share Posted September 16, 2009 It doesn't give a warning, even with: ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); added to the top. Also, this code: if (5 or (5/0) || 0) { echo "big"; } else { echo "small"; } also returns "big" even though it should be evaluating the division by zero first, and returning an error. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919458 Share on other sites More sharing options...
Zane Posted September 16, 2009 Share Posted September 16, 2009 what are you trying to do in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919468 Share on other sites More sharing options...
.josh Posted September 16, 2009 Share Posted September 16, 2009 Your condition is not actually comparing anything, so the only thing being evaluated is the existence of something. The condition will evaluate true by virtue of anything in there evaluating to something more than 0. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919475 Share on other sites More sharing options...
KevinM1 Posted September 16, 2009 Share Posted September 16, 2009 I think order of operations is still being performed correctly. Look at it in steps: if (5 or 0 || (5/0)) -> if ((5 or 0) || (5/0)) Like you said, the || is checked first. So, (5 or 0) is checked as the first part of that comparison, since logical operators have left associativity (i.e., left operand goes first). Within that comparison, 5 evaluates as true. Since PHP uses lazy evaluation, it doesn't even get to the illegal division. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919487 Share on other sites More sharing options...
antispin Posted September 16, 2009 Author Share Posted September 16, 2009 Nightslyr: Why would it be seen as ((5 or 0) || (5/0)) and not (5 or (0 || (5/0))) ? Shouldn't it default to the second interpretation due to operator precedence? Also, how does this apply to the second piece of code I posted? zanus: I'm just trying to figure out why it seems like the "or" is being evaluated before the "||" in this case, although it should be the other way around. I'm not trying to do anything practical other than figure out PHP handles "or" and "||" in practice. Crayon Violent: I know it evaluates to true, but shouldn't the division by zero be triggered? Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919720 Share on other sites More sharing options...
.josh Posted September 16, 2009 Share Posted September 16, 2009 no. nightslyr mostly got it right. php is lazy evaluation. But it first evaluate 0 || (5/0) and since 0 is false, the division by zero never gets evaluated. THEN it evaluates 5 or 0 which in turn evaluates true because 5 evaluates true. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919756 Share on other sites More sharing options...
antispin Posted September 16, 2009 Author Share Posted September 16, 2009 What about the second piece of code I posted, this one: if (5 or (5/0) || 0) { echo "big"; } else { echo "small"; } It also prints "big" without displaying an error. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919791 Share on other sites More sharing options...
antispin Posted September 16, 2009 Author Share Posted September 16, 2009 Besides which, your statement: "But it first evaluate 0 || (5/0) and since 0 is false, the division by zero never gets evaluated. " ...makes no sense. If 0 is false, PHP must evaluate the right-hand part of the logical statement in order to determine if the right-hand part is true. It can't just ignore it. That's not how || works. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919793 Share on other sites More sharing options...
.josh Posted September 17, 2009 Share Posted September 17, 2009 yeah i rethought it. My mistake. I think nightslyr was right in the first place: if (5 or 0 || (5/0)) -> if ((5 or 0) || (5/0)) so (5 or 0) makes true, and therefore (5/0) does not get evaluated. so even when it is: if (5 or (5/0) || 0) -> ((5 or (5/0)) || 0) (5 or (5/0)) that 5 still makes it true, and the (5/0) still doesn't get evaluated. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919821 Share on other sites More sharing options...
antispin Posted September 17, 2009 Author Share Posted September 17, 2009 I think that your/Nightslyr's logic makes sense, and I suspect that's what's happening, BUT: That's not what's supposed to happen! || is supposed to take precedence over "or" !! See: http://ca.php.net/manual/en/language.operators.precedence.php How can your interpretation -- which I agree, is what is actually happening -- be squared with the concept that || should be evaluated before "or" ?? You are adding parentheses to demonstrate what is actually occurring -- but there are no such parentheses in my original code. IMHO if I have: 5 or (5/0) || 0 then PHP's engine should first evaluate (5/0) || 0 and then look at 5 or (result of previous logical statement) Is PHP messing with me or what??? /bangs head against wall. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919827 Share on other sites More sharing options...
.josh Posted September 17, 2009 Share Posted September 17, 2009 || gets evaluated first, yes, but here's the thing: it starts off as "everything on the left" || "everything on the right" so it looks at that entire "5 or (5/0)" as the "left of the ||" So that whole thing gets evaluated first. Then within that, it evaluates the 5 as true, discarding the division by zero. Since 5 evaluates as true, "Everything on the right of ||" gets discarded, as well. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919828 Share on other sites More sharing options...
antispin Posted September 17, 2009 Author Share Posted September 17, 2009 Hey, that makes sense. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919832 Share on other sites More sharing options...
.josh Posted September 17, 2009 Share Posted September 17, 2009 ...which is exactly what nightslyr said in the first place. I thought he was a bit off, but then I was thinking about it again a bit later and was like well wait a minute, I know regex order of operations work the same as how nightslyr said, so....yeah... Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919834 Share on other sites More sharing options...
antispin Posted September 17, 2009 Author Share Posted September 17, 2009 The reason I found it confusing is because it's the opposite of the way arithmetic operator precedence works. For example: A + B * C You multiply B with C, then add A. Because multiplication has higher precedence, we evaluate it FIRST. On the other hand, with logical operators (now that you've both managed to bash it into my head): A or B || C We evaluate "A or B", then take the result (call it D) and do "D || C". Because "||" has higher precedence, we evaluate it LAST. You see? Opposite. Confusing. But, to me, no longer Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919837 Share on other sites More sharing options...
.josh Posted September 17, 2009 Share Posted September 17, 2009 well I'm glad you understand how it works, but fyi there is a flaw in your "opposite" thing... * is not the same operator as ||. A + B * C does the same thing in both math and in php. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919841 Share on other sites More sharing options...
.josh Posted September 17, 2009 Share Posted September 17, 2009 On the other hand, with logical operators (now that you've both managed to bash it into my head): A or B || C We evaluate "A or B", then take the result (call it D) and do "D || C". Because "||" has higher precedence, we evaluate it LAST. You see? Opposite. Confusing. But, to me, no longer And actually, even though you got to the finish line, you still don't quite get it on how you got there. || gets evaluated first, not last. Look at your example: A or B || C This gets evaluated as (A or B) || C You may think that it is evaluating last, but it's not. To demonstrate, let's flip the operators: A || B or C What do you think will happen there? This is what will happen: (A) || (B or C) A gets evaluated first, not the (B or C). If A evaluates true, (B or C) does not even get evaluated. So you see, || is getting evaluated first; PHP looks at it, then starts on the left side of it, then goes to the right side of it if it has to. Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-919843 Share on other sites More sharing options...
antispin Posted September 17, 2009 Author Share Posted September 17, 2009 You're right. Thanks for the clarification :-\ Quote Link to comment https://forums.phpfreaks.com/topic/174450-solved-operator-precedence-not-working/#findComment-920132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.