Bug #76931 [Opn->Nab]: foreach by reference copies last array element from second-last

From: Date: Mon, 24 Sep 2018 21:26:25 +0000
Subject: Bug #76931 [Opn->Nab]: foreach by reference copies last array element from second-last
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217225@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76931&edit=1 ID: 76931 Updated by: requinix@php.net Reported by: kellnerdenes at gmail dot com Summary: foreach by reference copies last array element from second-last -Status: Open +Status: Not a bug Type: Bug Package: *General Issues Operating System: Win64 PHP Version: 7.1.22 Block user comment: N Private report: N New Comment: http://php.net/manual/en/control-structures.foreach.php Big red warning. Should be hard to miss. Previous Comments: ------------------------------------------------------------------------ [2018-09-24 21:20:21] kellnerdenes at gmail dot com 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 this bug report at https://bugs.php.net/bug.php?id=76931&edit=1

« previous php.bugs (#217225) next »