Bug #76931 [NEW]: foreach by reference copies last array element from second-last
| From: | kellnerdenes at gmail dot com | Date: | Mon, 24 Sep 2018 21:20:21 +0000 |
| Subject: | Bug #76931 [NEW]: foreach by reference copies last array element from second-last | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-217224@lists.php.net to get a copy of this message | ||
From: kellnerdenes at gmail dot com
Operating system: Win64
PHP version: 7.1.22
Package: *General Issues
Bug Type: Bug
Bug description:foreach by reference copies last array element from second-last
Description:
------------
When looping along an array twice, first by reference, then by just
normal value, the last element gets affected (gets the value of the
second-last). So while it should display "first second third fourth", it
will give "first second third third" instead. Commenting any of the
foreaches or changing their order makes the problem disappear.
I've seen similar reports but I think this one is clearly a bug while
others seem to be unsure.
Test script:
---------------
$a = explode("\n","first\nsecond\nthird\nfourth");
foreach($a as &$x);
foreach($a as $x);
print_r($a);
Expected result:
----------------
Array
(
[0] => first
[1] => second
[2] => third
[3] => fourth
)
Actual result:
--------------
Array
(
[0] => first
[1] => second
[2] => third
[3] => third
)
--
Edit bug report at https://bugs.php.net/bug.php?id=76931&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76931&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76931&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76931&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=76931&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=76931&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=76931&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=76931&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=76931&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=76931&r=support
Expected behavior: https://bugs.php.net/fix.php?id=76931&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=76931&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=76931&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=76931&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76931&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=76931&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=76931&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=76931&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=76931&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=76931&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=76931&r=mysqlcfg