note 70845 added to language.operators
| From: | php-general at lists dot php dot net | Date: | Tue, 31 Oct 2006 18:20:48 +0000 |
| Subject: | note 70845 added to language.operators | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119271@lists.php.net to get a copy of this message | ||
In response to:
----------------------
T Chan
19-Aug-2006 10:30
[...]
In response to the manual, associativity doesn't determine the order of evaluation - it
determines how it's "bracketed": a=b=c=1 is equivalent to a=(b=(c=1)), but a is
evaluated first (an important point if evaluating a has side-effects)
<?php
function one($str) {
echo "$str";
return 1;
}
$a = array(1,2);
$a[one("A")] = $a[one("B")] = 1; // outputs AB
?>
----------------------
The manual, stated "associativity determines order of evaluation," which is completely
correct, as it uses a stack execution, as with C.
The example you provided to counter the manual's statement however, is contrived, as it uses
function calls to output a variable which is not even used in assignment; this in turn disproves (in
your opinion) the manual's statement. Fuction calls, have much higher precedence than the
assignment operator.
However:
Right Associativity with the '=' operator means the following:
The expression: $a = $b = 1;
IS indeed evaluated from right to left.
The stack is used as follows. (if you're not familiar with stacks / queues, look em up).
The expressions are pushed onto the stack in the following order:
$a $b = 1 =
^ ^
Bottom Top
The stack is then "evaluated" from top to bottom (which translates from right to left, if
you're reading the expression)
It would read: assign 1 to assign $b to $a
Which results in $a = 1
Try the following simple script:
<?php
function output(& $b)
{
echo( "b value is: ".$b );
return $b;
}
$a = 2;
$b = 5;
echo ($a = $b = 1);
$a = 2;
$b = 5;
echo ( "a value is: ".$a = output($b) );
?>
Output is:
1
b value is: 5
a value is: 5
---------------------------------------
Your example had nothing to do with the assignment operator evaluation order, only procedural
precedence.
----
Server IP: 66.163.161.117
Probable Submitter: 24.121.21.115
----
Manual Page -- http://www.php.net/manual/en/language.operators.php
Edit -- https://master.php.net/note/edit/70845
Del: integrated -- https://master.php.net/note/delete/70845/integrated
Del: useless -- https://master.php.net/note/delete/70845/useless
Del: bad code -- https://master.php.net/note/delete/70845/bad+code
Del: spam -- https://master.php.net/note/delete/70845/spam
Del: non-english -- https://master.php.net/note/delete/70845/non-english
Del: in docs -- https://master.php.net/note/delete/70845/in+docs
Del: other reasons-- https://master.php.net/note/delete/70845
Reject -- https://master.php.net/note/reject/70845
Search -- https://master.php.net/manage/user-notes.php