|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-04-27 17:04 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 13:00:01 2025 UTC |
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)