php - Why is it assigning value to $b? It should not as per operator precedence rule -
i trying answer why code infinite loop?
there thought issue because of operations precedence, when checked http://php.net/manual/en/language.operators.precedence.php , .
has more precedence =
so tried following code :
$a.$b = "test"; echo $a; echo $b;
and got undefined variable a
, test
means assigning value $b, how assigning value $b (should not per operations precedence)
i believe answer stated in docs:
although = has lower precedence other operators, php still allow expressions similar following:
if (!$a = foo())
, in case return value offoo()
put$a
.
expanding on jon's answer, since can't assign expression, =
takes precedence , interpreter sees following statement:
$a.($b = "test");
so become 2 separate expressions, each following own precedence. , therefore test
assigned $b
.
to prove this, add assignment:
$a = 'my '; $c = $a.$b = "test"; var_dump($a); // string(3) "my " var_dump($b); // string(4) "test" var_dump($c); // string(7) "my test"
Comments
Post a Comment