#47400 [Opn]: Variable variables don't work with "global" inside a function
| From: | php at richardneill dot org | Date: | Mon, 16 Feb 2009 05:09:18 +0000 |
| Subject: | #47400 [Opn]: Variable variables don't work with "global" inside a function | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-1873@lists.php.net to get a copy of this message | ||
ID: 47400
User updated by: php at richardneill dot org
Reported By: php at richardneill dot org
Status: Open
Bug Type: Documentation problem
PHP Version: Irrelevant
New Comment:
Thanks for your response.
The thing is, I'm NOT accessing $eat2 via the $GLOBALS superglobals
array. All I'm doing is using the "global" keyword to bring it into
scope in *this* specific function.
The note here:
http://uk2.php.net/manual/en/language.variables.variable.php
is indeed suggestive that there might be a problem (that's why I filed
this as a probable documentation bug), but surely if it's $GLOBALS that
can't be used with variable variables, it means that I just can't write:
$$GLOBALS["eat2"] ?
Previous Comments:
------------------------------------------------------------------------
[2009-02-16 04:31:02] preinheimer@php.net
also: http://www.php.net/manual/en/language.variables.variable.php
Where could we have put this information to make it easier for you to
find? (so as to help other developers in the future)
------------------------------------------------------------------------
[2009-02-16 04:24:59] preinheimer@php.net
Does the third comment on this page address your concerns?
http://www.php.net/manual/en/language.variables.superglobals.php
------------------------------------------------------------------------
[2009-02-16 04:20:27] php at richardneill dot org
Description:
------------
Note sure whether this is an actual bug, or an omission from the
documentation. Anyway, it doesn't seem to work when we use "global" to
bring a variable into scope within a function, iff that variable name is
itself a variable variable.
The example below is the shortest possible demonstration of the problem
I can create (sorry it looks very contrived).
Thanks for your help.
Reproduce code:
---------------
<?
$fruit = "apple";
$lunch = "fruit";
$eat1 = $$lunch;
echo "Tasty: $eat1\n"; // apple, as expected
function nom ($meal) { //$meal contains the string 'lunch'
$eat2=$$meal; //$eat2 should be the variable $lunch
global $eat2; //...which we bring into scope using "global"
echo "Tasty: $eat2\n"; //Should give "apple", but doesn't.
}
nom ('lunch');
?>
Expected result:
----------------
Tasty: apple
Tasty: apple
Actual result:
--------------
Tasty: apple
Tasty:
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47400&edit=1