#27395 [Asn]: arrays cannot be passed by value

From: Date: Wed, 25 Feb 2004 18:39:51 +0000
Subject: #27395 [Asn]: arrays cannot be passed by value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-55557@lists.php.net to get a copy of this message
 ID:               27395
 Updated by:       sniper@php.net
 Reported By:      jamesn at tocquigny dot com
 Status:           Assigned
 Bug Type:         Scripting Engine problem
 Operating System: *
 PHP Version:      4CVS, 5CVS (2004-02-25
 Assigned To:      zeev
 New Comment:

Wrong test, this was the one:

<?php

function theFunction($arg) {
    $arg[0] = 2;
}

// Reference on array index
$arr1 = array (1);
$reference1 =& $arr1[0];
    
var_dump($reference1);
var_dump($arr1);
theFunction($arr1);
var_dump($reference1);
var_dump($arr1);

echo "--------\n";

// Reference on array
$arr2 = array (1);
$reference2 =& $arr2;
    
var_dump($reference2);
var_dump($arr2);
theFunction($arr2);
var_dump($reference2);
var_dump($arr2);

?>



Previous Comments:
------------------------------------------------------------------------

[2004-02-25 13:26:08] sniper@php.net

Here's a bit better test script:

<?php

function theFunction($arg) {
    $arg[0] = 2;
}

$arr1 = array (1);
$reference =& $arr1[0];

var_dump($reference);
var_dump($arr1);
theFunction($arr1);
var_dump($reference);
var_dump($arr1);

?>


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

[2004-02-25 12:02:02] jamesn at tocquigny dot com

Description:
------------
There appears to be a problem with php that doesn't allow arrays to be
passed by value.  The fix appears to be something similar to:
http://bugs.php.net/bug.php?id=6417
if (PZVAL_IS_REF(*p))
{
   SEPARATE_ZVAL(p);
} else {
   zval_add_ref(p);
}

It would appear that that logic is missing in one place or another.  I
could not track it down myself.  Code similar to this appears to be
copy/pasted in various places.

Reproduce code:
---------------
$array = array(1);

// This line makes the call to theFunction() act as if passed by ref.?
$reference =& $array[0];

echo $array[0], '<br>';
theFunction($array);

echo $array[0], '<br>';

function theFunction($array) {
    $array[0] = 2;
}

Expected result:
----------------
you should get 1 and 1

Actual result:
--------------
instead you get 1 and 2!


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


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


Thread (10 messages)

« previous php.bugs (#55557) next »