php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62110 Foreach creates a copy of the array passed by value in function
Submitted: 2012-05-22 19:56 UTC Modified: 2012-05-24 10:15 UTC
From: a dot protaskin at gmail dot com Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.4.3 OS: Arch Linux
Private report: No CVE-ID: None
 [2012-05-22 19:56 UTC] a dot protaskin at gmail dot com
Description:
------------
I also tried to execute this code on PHP 5.3.13 and PHP 5.2.17 with same result.

All options are default.

Test script:
---------------
http://pastebin.com/caczCxuD

Expected result:
----------------
Start: 786432
Inner by_reference: 786432
Inner foreach: 786432
After by_reference: 786432
Inner by_value: 786432
Inner foreach: 786432
After by_value: 786432

Actual result:
--------------
Start: 786432
Inner by_reference: 786432
Inner foreach: 786432
After by_reference: 786432
Inner by_value: 786432
Inner foreach: 1310720
After by_value: 104857

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-23 21:59 UTC] nikic@php.net
The foreach loop changes the internal array pointer. If the array zval wouldn't be separated the loop thus would thus affect he array which was passed by value, which is obviously not acceptable.
 [2012-05-24 00:14 UTC] a dot protaskin at gmail dot com
Thanks for your answers, nikic. I read your answer in the neighbouring entry and your articles too. I understood this behaviour, this is not the bug.

Sorry for my bad english.
 [2012-05-24 10:15 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2012-05-24 10:15 UTC] johannes@php.net
As said above.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC