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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: a dot protaskin at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sun Dec 22 06:01:30 2024 UTC