#36018 [Opn]: variable variable reference to superglobal only works after non-variable ref

From: Date: Sun, 15 Jan 2006 11:12:41 +0000
Subject: #36018 [Opn]: variable variable reference to superglobal only works after non-variable ref
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969371604@lists.php.net to get a copy of this message
ID: 36018 Updated by: derick@php.net Reported By: ozone at cname dot com Status: Open -Bug Type: Variables related +Bug Type: Documentation problem Operating System: linux 2.6.13.2 PHP Version: 5.1.2 New Comment: The behavior is expected. PHP will only handle super globals correctly if use their full name. The documentation is wrong on this. (The reason for this particular behavior is that PHP will only populate the super globals when they are first used, but PHP can only detect that when you're using the full name only). Previous Comments: ------------------------------------------------------------------------ [2006-01-15 06:38:35] ozone at cname dot com Description: ------------ environment: generic apache 2.0.55 / php 5.1.2 on linux (can reproduce with 5.0.5 on solaris9; it's not the os) yes, i've read the "variable variable references to superglobals aren't supported in functions"; this isn't in a function, and the behavior is very very strange. see attached code. the var-var-refs to $_SERVER works as expected IF AND ONLY IF that superglobal is referred to elsewhere in the code by its real name -- anywhere in the code is fine, even after the var-var-ref. this behavior suggests that the $_SERVER superglobal isn't instantiated (or made available) if the interpreter decides (wrongly) that it's not necessary. the parser does complain thusly: PHP Warning: Invalid argument supplied for foreach() in /home/web/docs/bug.php on line 11 Reproduce code: --------------- <? // works as expected if the following line is uncommented // if(is_array($_SERVER)) echo ''; echo "<html><table border=1 cellpadding=2>"; foreach(array( "_GET" => "#f0c0f0", "_POST" => "#f0c0c0", "_SESSION" => "#c0f0f0", "_SERVER" => "#f0f0c0") as $a => $c) { echo "\n<tr width=100% bgcolor=#c0c0c0><td colspan=2 align=right>$a</td></tr>"; foreach(${$a} as $k => $v) if($k) echo "\n<tr><td bgcolor=$c>$k</td><td bgcolor=$c>$v</td></tr>"; } echo "\n</table></html>"; ?> Expected result: ---------------- should have a table of all variables related to the request (i use this bit of code where i catch errors, it tends to be helpful). Actual result: -------------- nothing. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=36018&edit=1

« previous php.doc (#969371604) next »