Bug #69784 [Com]: bad variable reference

From: Date: Wed, 10 Jun 2015 09:49:57 +0000
Subject: Bug #69784 [Com]: bad variable reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-193285@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69784&edit=1

 ID:                 69784
 Comment by:         nikic@php.net
 Reported by:        artur at salonstron dot pl
 Summary:            bad variable reference
 Status:             Open
 Type:               Bug
 Package:            Variables related
 Operating System:   irrelevant
 PHP Version:        5.6.9
 Block user comment: N
 Private report:     N

 New Comment:

According to http://3v4l.org/L8GsZ 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?


Previous Comments:
------------------------------------------------------------------------
[2015-06-10 09:43:54] 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:21:32] nikic@php.net

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-09 11:25:57] artur at salonstron dot pl

Description:
------------
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:
---------------
<?php
$a = array (
    1=>"a",
    2=>"b",
    3=>"c"
);
$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:
----------------
d:
at3 bt3 ct3

Actual result:
--------------
d:


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=69784&edit=1


Thread (8 messages)

« previous php.bugs (#193285) next »