Jump to content

Recommended Posts

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

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.

 

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.

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?

 

 

 

 

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.

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.

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.

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.

|| 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.

 

 

...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...

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 :)

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.