php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31899 The variable variable doesn't contain the value that logic should permit
Submitted: 2005-02-09 16:08 UTC Modified: 2005-02-10 17:30 UTC
From: php at simplicate dot it Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.8 OS: Linux 2.4.22-1.2199 - Apache 2.0
Private report: No CVE-ID: None
 [2005-02-09 16:08 UTC] php at simplicate dot it
Description:
------------
When a variable variable is globalized into a function and then its value is set to a reference of another variable, the variable variable's value does not extend past that of the function.

Reproduce code:
---------------
$v = 'albert';

function test($var){

	global $$var, $v;

	$$var = &$v;

}

test('mmm');

var_dump($mmm);
exit;

// for further examples follow the link below:
// http://www.ourproperty.co.uk/test/index.php?code=0&allowin=SECurePHRase808


Expected result:
----------------
string(6) "albert" 

Actual result:
--------------
NULL

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-09 16:18 UTC] php at simplicate dot it
In order for the link i posted to work it needs to include the "08" on the end... altho it has been wrapped by your parser function, just making sure - it maybe quite obvious but i really want this problem looked into / commented on and not cast aside.

http://ourproperty.co.uk/test/index.php?code=0&allowin=SECurePHRase808
 [2005-02-09 22:37 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$$var = &$v;

resolves to

$(value of $mmm) = &$v;

Since $mmm contains no value nothing happens.
 [2005-02-10 01:35 UTC] php at simplicate dot it
What? sorry i really don't understand what you are talking about.. i am trying to set the value of $mmm... thats why it currently has no value... the behaviour of $$var should be $(value of $var) which is 'mmm' so the variable variable should resolve to $('mmm') not the value of $mmm as that would be rediculous, talk about recurssion!!!

Anyway that is beside the point - i know i am correct because the whole thing works when not in the confines of a function. All this information can be found on the link that i posted.

	$v	= 'albert';
	$var	= 'mmm';
	$$var	= &$v;

This works fine...! so why putting it within a function does anything change... unless you can't safely globalize a variable variable... (ie. global $$var - but this works for other values) or variable variabling a reference changes the behaviour of the variable variable????

i couldn't find any information about this problem / feature in the references section or the variable variables section so where else can i look, i even searched the whole site for anything along the same lines??? And i reject the insinuation that i didn't check it in the first place...

It also works if i remove the referencing to $v and just copy $v's value:

	$v = 'albert';

	function test($var){

		global $$var, $v;

		$$var = $v; // <--- no referencing!!

	}

	test('mmm');

	var_dump($mmm);
	exit;

The above code works fine and $mmm is set to the correct value "albert" outside of the functions reach.

So what is going on, please can you give me a better explaination? this problem causes me no hinderance what-so-ever as i can code around it to my hearts content but anything that i see as a flaw in logic leaves me to worry about the reliability of a language. You may be a volenteer and you probably have to wade through a lot of requests, but if a jobs worth doing its worth doing well and i really think that you haven't fully looked into what i "took the time" to write.

Thank you
 [2005-02-10 14:54 UTC] sniper@php.net
RTFM: http://www.php.net/manual/en/language.variables.scope.php

And read the part about "References with global and static variables" 


 [2005-02-10 17:30 UTC] php at simplicate dot it
Ah ok... thank you... could have just included that link before... 

I am not a PHP guru, but i'm not stupid either - i am an average user of PHP and i assumed it was a behaviour of variable variables and not that it was to do with the global statement - if i assumed that then so will others - so please can something be added to the variable variables page.

If that had existed then it would have helped a great deal... and i wouldn't have needed to bother you. i would post it myself but you guys seem very strict on what is allowed to be displayed...

oh and i did RTFM... but if you aren't sure what you are looking for then its very hard to find it in TFM, especially when TFM's search relies on google. and doesn't have it's own tailored search that is intelligent to the sites data and on what people search for. if it did it might actually help you guys - anyway - you guys are THE PHP resource site, why aren't you using PHP to search?

As this side-effect is so unexpected i think more attention should have been brought to it. The more places it is written, the less people will be confused, the less they will bother you, is it possible to leave this as a bogus bug so that when someone tries to submit a bug along the same lines it warns them about the problem? just like it did for me when i tried to post this bug - altho that time it was nothing to do with what i was talking about. Then they can avoid getting such blunt responses, where a few more words would have actually sped up the whole process.

Thanks for your time all the same, and i hope the volunteering gets more enjoyable for you all.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Aug 18 19:01:28 2024 UTC