php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30345 Unable to access predifined variables as variable variables from functions
Submitted: 2004-10-06 23:20 UTC Modified: 2004-10-07 09:09 UTC
From: talex_id at o2 dot ru Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3.8 OS: FreeBSD
Private report: No CVE-ID: None
 [2004-10-06 23:20 UTC] talex_id at o2 dot ru
Description:
------------
PHP allows to use 'variable variables', constructions, like a $$name or ${$name}, where $name is a string. But if i try to access to $GLOBALS from function using this method ($$name or ${$name}, where $name = 'GLOBALS') i can't get it until i use its name at least one time into function (or method) body. It's enouth to add string $GLOBALS; (to 'touch') to make it visible from $$name construction. Another predifned variables are unaccessible even if i set some values to them into function body. See code examples (http://talex-id.o2.ru/devel/php/bugs/predefined_variables1.php.txt and http://talex-id.o2.ru/devel/php/bugs/predefined_variables2.php.txt). I used CLI version of PHP to test them.


Reproduce code:
---------------
<?php
function f1()
{	
	echo 'GLOBALS, direct : ' . $GLOBALS . "\n";
	echo 'GLOBALS, string in "{}" : ' . ${'GLOBALS'} . "\n";
} 
function f2()
{
	$g_name = 'GLOBALS';
	echo 'GLOBALS, string from variable in "{}" : ' . ${$g_name} . "\n"; 
} 
function f3()
{	
	$g_name = 'GLOBALS';
	$GLOBALS; // Now we just have to touch GLOBALS variable to make it visible
	echo 'GLOBALS, toched, string from variable in "{}" : ' . ${$g_name} . "\n"; 
} 
f1();
echo "----------------------------------------------------\n";
f2();
echo "----------------------------------------------------\n";
f3();
?> 

Expected result:
----------------
GLOBALS, direct : Array
GLOBALS, string in "{}" : Array
----------------------------------------------------
GLOBALS, string from variable in "{}" : Array
----------------------------------------------------
GLOBALS, toched, string from variable in "{}" : Array


Actual result:
--------------
GLOBALS, direct : Array
GLOBALS, string in "{}" : Array
----------------------------------------------------
GLOBALS, string from variable in "{}" :
----------------------------------------------------
GLOBALS, toched, string from variable in "{}" : Array


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-07 09:09 UTC] derick@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

You can\'t use the superglobals as variable variables, that\'s in the manual.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Feb 15 22:00:01 2026 UTC