|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69784 bad variable reference
Submitted: 2015-06-09 11:25 UTC Modified: 2017-10-19 21:35 UTC
Avg. Score:4.2 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:3 (75.0%)
From: artur at salonstron dot pl Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.6.9 OS: irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
43 - 20 = ?
Subscribe to this entry?

 [2015-06-09 11:25 UTC] artur at salonstron dot pl
In the example below, the variable $d is assigned a reference to a local variable $a (in function test) despite the fact that variable was not returns by reference.

To fix this, you must either before returning the $a use the reset(), or inside test() use another variable.

Test script:
$a = array (
$d = test($a);
echo "d:<br />";
while(list($key, $val)=each($d))  {
    echo $val." ";

function test($a) {
    while(list($key, $val)=each($a)) {
        $a[$key] = $val . "t3";

    return $a;

Expected result:
at3 bt3 ct3

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-10 09:21 UTC]
Sorry, I'm not sure I see what the issue is here. In test() you're modifying the internal array pointer (IAP) of $a and then returning $a (including the modified IAP). Then in the main script you assign this to $d (still with modified IAP that is at the end of the array) and try to iterate further with it - which fails. Isn't this how it is supposed to work?
 [2015-06-10 09:43 UTC] artur at salonstron dot pl
in previous versions of php script worked differently. The variable $d was assigned to different array with unmodified IAP.
It seems to me that it is a a bug because if I wanted to return a reference to the array, which is inside a function test() I would done it differently.
Sorry for my english, I hope that everybody understands what I mean.
 [2015-06-10 09:49 UTC]
According to this behavior has stayed the same for all PHP versions - apart from a short regression interval in PHP 7. Are you sure the code you tested on older versions wasn't in some way different?
 [2015-06-10 10:10 UTC] artur at salonstron dot pl
Currently I'm using: PHP Version 5.5.11 on windows. On most of my clients servers php version is 5.2-5.5. Recently, one of the servers has been updated to version 5.6.9 and the difference is just there.

See this example: its output of the same script + phpversion()
 [2015-06-10 10:14 UTC] artur at salonstron dot pl
Customer servers running on different Linux distributions.
 [2015-06-10 11:14 UTC] artur at salonstron dot pl
I run some other tests and it seems that on servers with php 5.2.* 5.3.* 5.4.* script works as I expected on servers with php 5.6.* it work different
 [2017-10-19 21:35 UTC]
-Status: Open +Status: Not a bug
 [2017-10-19 21:35 UTC]
Closing this, as the provided code behaves as intended. I can't say why you are observing different behavior on older versions, but the current behavior is definitely correct.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 12:01:33 2024 UTC