Bug #79459 [Com]: foreach twice with(out) assignment error
| From: | fauvel dot marc at free dot fr | Date: | Wed, 08 Apr 2020 15:57:33 +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-226493@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:
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!
Previous Comments:
------------------------------------------------------------------------
[2020-04-08 15:30:32] 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:27:17] 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:24:05] 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: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
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=79459
--
Edit this bug report at https://bugs.php.net/bug.php?id=79459&edit=1