List Info

Thread: note 70845 added to language.operators




note 70845 added to language.operators
user name
2006-10-31 18:20:48
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 -- h
ttp://www.php.net/manual/en/language.operators.php
Edit        -- https://master
.php.net/note/edit/70845
Del: integrated  -- h
ttps://master.php.net/note/delete/70845/integrated
Del: useless     -- http
s://master.php.net/note/delete/70845/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/70845/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/70845
Reject      -- https://mast
er.php.net/note/reject/70845
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )