# left/right order of assignment and ++ increment changed? 5.0.x --> 5.1.x

- Thread: https://externals.io/message/23633
- Original archive: https://news-web.php.net/php.internals/23633
- Started by D. Dante Lorenso on 2006-05-24T16:26:09+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #23633 — D. Dante Lorenso — 2006-05-24T16:26:09+00:00

URL: https://externals.io/message/23633#23633

Internals,

Seems the order or left vs right assignment evaluation has changed 
somehow recently in my upgrade to PHP 5.1.4.  See the following code:

---------- 8< -------------------- 8< ----------
<?php
$data = array();
$index = 0;
for ($x = 0; $x < 5; $x++) {
   $data[$index] = $index++;
}
print_r($data);
?>
---------- 8< -------------------- 8< ----------

in PHP 5.0.5:

Array
(
   [0] => 0
   [1] => 1
   [2] => 2
   [3] => 3
   [4] => 4
)

in PHP 5.1.4:

Array
(
   [1] => 0
   [2] => 1
   [3] => 2
   [4] => 3
   [5] => 4
)

Is this a bug, a feature, or bad programmer [me]?

Dante

---

## #23634 — Michael Wallner — 2006-05-24T16:28:44+00:00

URL: https://externals.io/message/23633#23634 · In reply to: #23633

D. Dante Lorenso wrote:
> Internals,
> 
> Seems the order or left vs right assignment evaluation has changed 
> somehow recently in my upgrade to PHP 5.1.4.  See the following code:
> 
> ---------- 8< -------------------- 8< ----------
> <?php
> $data = array();
> $index = 0;
> for ($x = 0; $x < 5; $x++) {
>   $data[$index] = $index++;
> }

The behaviour is undefined (tm) if you use the variable to be in/decremented more than once in a statement.

Regards,
-- 
Michael

---

## #23635 — Sara Golemon — 2006-05-24T17:49:43+00:00

URL: https://externals.io/message/23633#23635 · In reply to: #23633

> Seems the order or left vs right assignment evaluation has changed somehow 
> recently in my upgrade to PHP 5.1.4.  See the following code:
>
That's is indeed a quirk of a change in the way variables are 
retreived/stored between 5.0 and 5.1.

Is it an unexpected BC break? In functional terms yes, but technically no. 
As mike already replied, using the same var more than once in a single 
expression where the value of the variable is expected to change is (and has 
always been) considered "undefined behavior".  I'd recommend changing your 
statement to:
$data[$index] = $index;
++$index;

And before you complain about the "extra work" for the engine, let me just 
mention this is still lighter weight than what 5.0 and 4.x were doing.

-Sara

---

## #23642 — D. Dante Lorenso — 2006-05-25T06:48:47+00:00

URL: https://externals.io/message/23633#23642 · In reply to: #23635

Sara Golemon wrote:
> > […]
> That's is indeed a quirk of a change in the way variables are 
> retreived/stored between 5.0 and 5.1.
> Is it an unexpected BC break? In functional terms yes, but technically 
> no. As mike already replied, using the same var more than once in a 
> single expression where the value of the variable is expected to 
> change is (and has always been) considered "undefined behavior".  I'd 
> recommend changing your statement to:
> $data[$index] = $index;
> ++$index;
>
> And before you complain about the "extra work" for the engine, let me 
> just mention this is still lighter weight than what 5.0 and 4.x were 
> doing.

No complaints here.  This is a change I can easily work around and don't 
mind the BC breakage.  Just wanted to illuminate the error to internals 
just in case it would reflect upon possible breakage elsewhere.  I 
worked around the undefined behavior before the first email was sent ;-)

Keep up the good work all.

Dante
