note 110914 added to language.operators.increment

From: Date: Sat, 22 Dec 2012 02:54:56 +0000
Subject: note 110914 added to language.operators.increment
Groups: php.notes 
Request: Send a blank email to php-notes+get-192638@lists.php.net to get a copy of this message
Regarding post increment and decrement in the index of an array (and I assume strings) used in assignment and expression to be assigned: Assignment index expressions and value expressions are both evaluated before any actual assignment happens, that means that no mater where you place your post incr/decr you may not be getting the result you had in mind. The following example shows this. $a = [ 0, 1, 2 ]; $i = 0; $a[$i++] = $i; var_dump( $a ); /* renders: ( note element [initial value $i as 0] is assign to [after incr value $i as 1] as I would not expect ) array (size=3) 0 => int 1 1 => int 1 2 => int 2 */ $a = [ 0, 1, 2 ]; $i = 0; $a[$i] = $i++; var_dump( $a ); /* renders: ( note element [after incr value $i as 1] is assign to [initial value $i as 0] as expected ) array (size=3) 0 => int 0 1 => int 0 2 => int 2 */ There is a note on this already, but it doesn't really explain the problem and the sample code is missing initial values, so I am posting this because it is more to the point and clearer. ---- Server IP: 69.147.83.199 Probable Submitter: 125.162.134.91 ---- Manual Page -- http://php.net/manual/en/language.operators.increment.php Edit -- https://master.php.net/note/edit/110914 Del: integrated -- https://master.php.net/note/delete/110914/integrated Del: useless -- https://master.php.net/note/delete/110914/useless Del: bad code -- https://master.php.net/note/delete/110914/bad+code Del: spam -- https://master.php.net/note/delete/110914/spam Del: non-english -- https://master.php.net/note/delete/110914/non-english Del: in docs -- https://master.php.net/note/delete/110914/in+docs Del: other reasons-- https://master.php.net/note/delete/110914 Reject -- https://master.php.net/note/reject/110914 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#192638) next »