[PHP-BUG] Bug #77397 [NEW]: Reference variable in foreach

[email protected] ("shranet at gmail dot com") Wed, 02 Jan 2019 10:06:28 +0000
Newsgroups php.standards
Message-ID <[email protected]>
From:             shranet at gmail dot com
Operating system: MacOS
PHP version:      7.2.13
Package:          PHP Language Specification
Bug Type:         Bug
Bug description:Reference variable in foreach

Description:
------------
When using same variable with reference and without reference in foreach
works incorrectly.

Test script:
---------------
$data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

foreach($data as $k => &$v) {
    rsort($v);
}

foreach($data as $k => $v) {
    print_r($v);
}

Expected result:
----------------
Array
(
    [0] => 3
    [1] => 2
    [2] => 1
)
Array
(
    [0] => 6
    [1] => 5
    [2] => 4
)
Array
(
    [0] => 6
    [1] => 5
    [2] => 4
)

Actual result:
--------------
Array
(
    [0] => 3
    [1] => 2
    [2] => 1
)
Array
(
    [0] => 6
    [1] => 5
    [2] => 4
)
Array
(
    [0] => 9
    [1] => 8
    [2] => 7
)


-- 
Edit bug report at https://bugs.php.net/bug.php?id=77397&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=77397&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=77397&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=77397&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=77397&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=77397&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=77397&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=77397&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=77397&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=77397&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=77397&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=77397&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=77397&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=77397&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77397&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=77397&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=77397&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=77397&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77397&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=77397&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=77397&r=mysqlcfg