php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79459 foreach twice with(out) assignment error
Submitted: 2020-04-08 14:50 UTC Modified: 2020-04-08 15:27 UTC
From: fauvel dot marc at free dot fr Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.4 OS: Docker (official)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: fauvel dot marc at free dot fr
New email:
PHP Version: OS:

 

 [2020-04-08 14:50 UTC] 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];

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-08 14:52 UTC] fauvel dot marc at free dot fr
-Operating System: Dcoker (official) +Operating System: Docker (official)
 [2020-04-08 14:52 UTC] fauvel dot marc at free dot fr
fix typo on os
 [2020-04-08 14:57 UTC] 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 15:03 UTC] sjon@php.net
-Status: Open +Status: Not a bug
 [2020-04-08 15:03 UTC] 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 15:24 UTC] fauvel dot marc at free dot fr
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.
 [2020-04-08 15:27 UTC] nikic@php.net
Please see the first "Warning" box on https://www.php.net/manual/en/control-structures.foreach.php for a slightly more detailed explanation. It's a common issue :)
 [2020-04-08 15:30 UTC] bugreports at gmail dot com
> I completly understand the side effect of references 

no

> i'm a 9 years cpp developper

don't help you when it comes to the ZendEngine

> Also the usefull of reference is speed

so you don't understand references in PHP because trying to be smarter than the ZendEngine is in most cases *slower*

http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html

> It avoid to duplicate content

it don't

> (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)

the zendenigine *is* copy-on-write based
http://www.phpinternalsbook.com/php5/zvals/memory_management.html
 [2020-04-08 15:57 UTC] fauvel dot marc at free dot fr
Sorry for my lack of knowledge.
I've read what you send, and effectively, i will change my way to work with php.

Also i've made a quick benchmark with 1.000.000 values to compare key assignment and reference assignment, and it gave the same range of time.

Thanks for your time!
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Aug 17 03:05:44 2022 UTC