Bug #79459 [Com]: foreach twice with(out) assignment error

From: Date: Wed, 08 Apr 2020 15:24:05 +0000
Subject: Bug #79459 [Com]: foreach twice with(out) assignment error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226489@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79459&edit=1 ID: 79459 Comment by: fauvel dot marc at free dot fr Reported by: fauvel dot marc at free dot fr Summary: foreach twice with(out) assignment error Status: Not a bug Type: Bug Package: *General Issues Operating System: Docker (official) PHP Version: 7.4.4 Block user comment: N Private report: N New Comment: I completly understand the side effect of references (i'm a 9 years cpp developper). My problem is that the second loop dont alter the pointer address of $b at all. It wont change anything for me, i've found a workaround. But it's not a natural behavior. Also the usefull of reference is speed. It avoid to duplicate content (in case of very long string for example, instead there is a "copy on write" behavior in php that i'm not aware of (whitch is possible). It also avoid the time of hastable lookup ($a[$key]) when all values need to be edited. Previous Comments: ------------------------------------------------------------------------ [2020-04-08 15:03:25] sjon@php.net Sorry, but your problem does not imply a bug in PHP itself. For a list of more appropriate places to ask for help using PHP, please visit http://www.php.net/support.php as this bug system is not the appropriate forum for asking support questions. Due to the volume of reports we can not explain in detail here why your report is not a bug. The support channels will be able to provide an explanation for you. Thank you for your interest in PHP. you should unset($b); between the two foreach loops if you want to remove the reference ------------------------------------------------------------------------ [2020-04-08 14:57:59] bugreports at gmail dot com after the first loop $b is still a reference to the last item in $a, that's why you should avoid references unlinke there are damned good reasons and most of the time there aren't sounds like a tpyical "i didn't understand teh side effects of references" case ------------------------------------------------------------------------ [2020-04-08 14:52:21] fauvel dot marc at free dot fr fix typo on os ------------------------------------------------------------------------ [2020-04-08 14:50:46] fauvel dot marc at free dot fr Description: ------------ In the test script, I expect $a to finish with [1, 1, 1] but the actual result is [1, 1, 2] Now if i change in the second loop $b with $c, the problem solve itself. I think $b already exist so the second foreach dont recreate it. And the second problem is that foreach dont iterate the second time ( [2, 2, 2] would have be a strange result but acceptable) Test script: --------------- <?php $a = [0, 0, 0]; foreach($a as &$b) $b = 1; foreach($a as $b) $b = 2; print_r($a); ?> Expected result: ---------------- $a = [1, 1, 1]; Actual result: -------------- $a = [1, 1, 2]; ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79459&edit=1

« previous php.bugs (#226489) next »