note 70845 deleted from language.operators by danbrown

From: Date: Fri, 30 Jan 2009 20:41:23 +0000
Subject: note 70845 deleted from language.operators by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150000@lists.php.net to get a copy of this message
Note Submitter: ---- 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.

« previous php.notes (#150000) next »