php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #47400 Variable variables don't work with "global" inside a function
Submitted: 2009-02-16 04:20 UTC Modified: 2009-02-16 11:40 UTC
From: php at richardneill dot org Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
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 richardneill dot org
New email:
PHP Version: OS:

 

 [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:


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-16 04:24 UTC] 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:31 UTC] 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 05:09 UTC] php at richardneill dot org
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"]       ?
 [2009-02-16 11:40 UTC] kalle@php.net
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: 
}
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 05:00:01 2026 UTC