#39276 [NEW]: Unpredictable behaviour of references

From: Date: Fri, 27 Oct 2006 09:56:57 +0000
Subject: #39276 [NEW]: Unpredictable behaviour of references
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-104063@lists.php.net to get a copy of this message
From: plyrvt at mail dot ru Operating system: any PHP version: 5.1.6 PHP Bug Type: Variables related Bug description: Unpredictable behaviour of references 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" -- Edit bug report at http://bugs.php.net/?id=39276&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=39276&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=39276&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=39276&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=39276&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=39276&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=39276&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=39276&r=needscript Try newer version: http://bugs.php.net/fix.php?id=39276&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=39276&r=support Expected behavior: http://bugs.php.net/fix.php?id=39276&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=39276&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=39276&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=39276&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=39276&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=39276&r=dst IIS Stability: http://bugs.php.net/fix.php?id=39276&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=39276&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=39276&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=39276&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=39276&r=mysqlcfg

« previous php.bugs (#104063) next »