尝试通过引用赋值来理解运算符优先级
Trying to understand operator precedence with assign by reference
Operator precedence 表示顺序应为:+, &, =
。但是这段代码执行显示顺序是:&, =, +
$b = 1;
$a = & $b + print('print executed');
if ($a == 1)
echo ' but one was not added and error was not raised';
输出print executed but one was not added and error was not raised
为什么更改了这种情况的优先级?
P.S.
$a = new stdClass();
$c = &$a instanceof $a;
var_dump($c); // class stdClass#1 (0) {}
$b = $a instanceof $a;
var_dump($b); // bool(true)
可以说,这并不能真正回答您的问题,但请考虑以下代码:
$b = 1;
$a = &$b + 123;
操作码揭示了以下执行策略:
compiled vars: !0 = $b, !1 = $a
line # * op fetch ext return operands
-----------------------------------------------------------------------------
3 0 > ASSIGN !0, 1
4 1 ASSIGN_REF !1, !0
2 ADD ~2 , 123
3 FREE ~2
如您所见,通过引用进行赋值,加法存储在临时变量中,然后释放;基本上,一个空操作。
也许文档可以更清楚,但我无法想象这个特定代码会有意义的场景:)
Operator precedence 表示顺序应为:+, &, =
。但是这段代码执行显示顺序是:&, =, +
$b = 1;
$a = & $b + print('print executed');
if ($a == 1)
echo ' but one was not added and error was not raised';
输出print executed but one was not added and error was not raised
为什么更改了这种情况的优先级?
P.S.
$a = new stdClass();
$c = &$a instanceof $a;
var_dump($c); // class stdClass#1 (0) {}
$b = $a instanceof $a;
var_dump($b); // bool(true)
可以说,这并不能真正回答您的问题,但请考虑以下代码:
$b = 1;
$a = &$b + 123;
操作码揭示了以下执行策略:
compiled vars: !0 = $b, !1 = $a
line # * op fetch ext return operands
-----------------------------------------------------------------------------
3 0 > ASSIGN !0, 1
4 1 ASSIGN_REF !1, !0
2 ADD ~2 , 123
3 FREE ~2
如您所见,通过引用进行赋值,加法存储在临时变量中,然后释放;基本上,一个空操作。
也许文档可以更清楚,但我无法想象这个特定代码会有意义的场景:)