Why can't I assign the variable referenced by PHP?

$bar = 7; $foo =& $bar = 9; 

From a technical point of view, will it be judged from right to left? So: $ bar = 9; $ foo = & $ bar

In case someone wonders. The reason I do this on the same line is to avoid nail cutting.

+8
php
source share
2 answers

The assignment expression $bar = 9 does not return a reference to $bar (i.e. the variable itself); instead, it returns the integer value of 9 .

Or if you need a quote from manual :

The value of an assignment expression is the assigned value. That is, the value of "$ a = 3" is 3.

You cannot assign a link directly to a value, only for a variable that contains that value. Thus, your convenient single-line scanner comes out spectacularly, and you have to split it into two parts.

+13
source share

For the same reason that

 $bar =& 9; 

invalid (because the link may point to another variable, but not a constant / literal). See: http://www.php.net/manual/en/language.references.whatdo.php

+2
source share

All Articles