php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41214 referencing a reference parameter on a function-local variable does not work
Submitted: 2007-04-27 17:04 UTC Modified: 2007-04-27 17:15 UTC
From: php at warhog dot net Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.1 OS: Linux (Ubuntu 6.10/Linux 2.6.17)
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: php at warhog dot net
New email:
PHP Version: OS:

 

 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-27 17:11 UTC] tony2001@php.net
You're creating a reference to the variable that exists only in the function scope.
 [2007-04-27 17:15 UTC] php at warhog dot net
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)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Dec 01 13:00:01 2025 UTC