php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39276 Unpredictable behaviour of references
Submitted: 2006-10-27 09:56 UTC Modified: 2006-10-27 10:13 UTC
From: plyrvt at mail dot ru Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.1.6 OS: any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: plyrvt at mail dot ru
New email:
PHP Version: OS:

 

 [2006-10-27 09:56 UTC] plyrvt at mail dot ru
Description:
------------
Depending on context, references can have 3 different behaviours:
1. Reference to element of an array. Element becomes reference automatically until that reference exist. 
1a. Whole array is passed 'by value' and keys of array copy can be modified independently
1b. Element of an array can not be passed 'by value'. It is forced to be passed by reference.
2. Reference to regular variable. Variable does not become reference and can be passed 'by value'

Reproduce code:
---------------
<?php
function bla1($param){ $param['k1']="new!"; }
function bla2($param){ $param="new!";}

$a['k1']='foo';;
echo var_dump($a);
echo "<hr>";
$v=&$a['k1'];
echo var_dump($a);
echo "<hr>";
bla2($a['k1']);
echo var_dump($a);
echo "<hr>";
bla1($a);
echo var_dump($a);
echo "<hr>";
unset($v);
echo var_dump($a);

echo "<hr>";

$k1='foo';
echo var_dump($k1);
echo "<hr>";
$v=&$k1;
echo var_dump($k1);
echo "<hr>";
bla2($k1);
echo var_dump($k1);
echo "<hr>";
unset($v);
echo var_dump($k1);

?>

Expected result:
----------------
any predictable output

Actual result:
--------------
array(1) { ["k1"]=> string(3) "foo" } 
array(1) { ["k1"]=> &string(3) "foo" } 
array(1) { ["k1"]=> &string(3) "foo" } 
array(1) { ["k1"]=> &string(4) "new!" } 
array(1) { ["k1"]=> string(4) "new!" } 
--------------
string(3) "foo" 
string(3) "foo" 
string(3) "foo" 
string(3) "foo"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-27 10:06 UTC] plyrvt at mail dot ru
Sorry, 1a & 1b were messed up. Correct is:

1a. Element of an array is passed 'by value' and can be modified independently
1b. Whole array can not be passed 'by value'. It is forced to be passed by reference.
 [2006-10-27 10:13 UTC] tony2001@php.net
This is expected and predictable.
When an array contains references, they still remain references, even if the array is copied.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 12:01:30 2024 UTC