|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-02-16 04:20 UTC] 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:
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 06:00:01 2026 UTC |
Bogus, because your example is wrong: function nom ($meal) { // Say $meal = 'lunch' $eat2=$$meal; // Here you get the value of the variable '$lunch' because $$meal = $lunch, and since $lunch isn't in this scope an E_NOTICE will be generated and $eat2 will be NULL global $eat2; // Globalizing the variable $eat2 which doesn't exists echo "Tasty: $eat2\n"; // Output: Tasty: }