在这个PHP表达式周围加上括号会改变结果。 为什么?

These two lines produce different results:

$r1= true xor true ;
$r2=(true xor true);

var_dump($r1);
var_dump($r2);

Output:

bool(true)
bool(false)

Why?

Codepen example: http://codepad.org/O4Kn1YVa

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

= is higher priority than xor

$r1 = true xor true ;

=>

($r1 = true) xor true ; // "=" has highest priority

=>

$r1 xor true ; // and only now xor

In this case you do not write result of xor anywhere and have lost it.

In case with result 2 you are forcing the execution order by parentheses.

All parentheses do is to enforce the precedence. So if adding them makes a difference, you've changed the precedence / execution order.

Which is the case here: http://php.net/manual/en/language.operators.precedence.php