#41214 [Bgs]: referencing a reference parameter on a function-local variable does not work

From: Date: Fri, 27 Apr 2007 17:15:53 +0000
Subject: #41214 [Bgs]: referencing a reference parameter on a function-local variable does not work
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-112011@lists.php.net to get a copy of this message
ID: 41214 User updated by: php at warhog dot net Reported By: php at warhog dot net Status: Bogus Bug Type: Variables related Operating System: Linux (Ubuntu 6.10/Linux 2.6.17) PHP Version: 5.2.1 New Comment: Same thing in PHP 5.1.6 (from the official ubuntu repositories). I thought about the problem and considered the fact that a local variable vanishes when the function is done... so I tried the same with a static variable.. but it didn't work either: <?php function some_function(&$foo) { static $_foo = NULL; if (is_null($_foo)) { $_foo = 1; } var_dump($_foo); $foo =& $_foo; var_dump($foo); } function some_other_function(&$foo) { $foo = 2; } function yet_another_function(&$foo) { $_foo = 3; $foo =& $_foo; var_dump($foo); } some_function($bar); var_dump($bar); some_other_function($bar); var_dump($bar); yet_another_function($bar); var_dump($bar); ?> Expected: --------- int(1) int(1) int(1) int(2) int(3) int(3) Actual Result: -------------- int(1) int(1) NULL int(2) int(3) int(2) Previous Comments: ------------------------------------------------------------------------ [2007-04-27 17:11:24] tony2001@php.net You're creating a reference to the variable that exists only in the function scope. ------------------------------------------------------------------------ [2007-04-27 17:04:28] php at warhog dot net Description: ------------ When having a reference-variable as parameter in a function and referencing it on a local variable (inside the function) the variable will keep the referenced value inside the function but not outside. That's not the way I understand references in PHP. Reproduce code: --------------- <?php function yet_another_function(&$foo) { $_foo = 3; $foo =& $_foo; var_dump($foo); } yet_another_function($bar); var_dump($bar); ?> Expected result: ---------------- int(3) int(3) Actual result: -------------- int(3) NULL ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41214&edit=1

« previous php.bugs (#112011) next »