Bug #54131 [Opn->Bgs]: _SERVER, _ENV access via $$ shows Notice
Edit report at http://bugs.php.net/bug.php?id=54131&edit=1
ID: 54131
Updated by: rasmus@php.net
Reported by: daniel-marschall at viathinksoft dot de
Summary: _SERVER, _ENV access via $$ shows Notice
-Status: Open
+Status: Bogus
Type: Bug
Package: Unknown/Other Function
Operating System: Debian
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
This is not a bug. It is a documented optimization feature. See
http://php.net/manual/en/ini.core.php and look
for the section on
auto_globals_jit
with the big pink warning which says, "Usage of SERVER and ENV variables
is
checked during compile time so using them through e.g. variable
variables will not
cause their initialization."
Previous Comments:
------------------------------------------------------------------------
[2011-03-02 05:09:42] daniel-marschall at viathinksoft dot de
I have found something additional.
(1) You can also "touch" the variable with simple
<?php
$_SERVER;
?>
(2) You do not have to touch the Variable BEFORE you want to access it
wit $$x.
Actually, you just need to touch it ANYWHERE inside the script. Then,
the PHP
interpreter will "activate" $_SERVER.
This scripts will throw an notice:
<?php
error_reporting(E_ALL | E_STRICT);
$x = '_SERVER';
print_r($$x);
?>
These scripts will not throw an notice
<?php
error_reporting(E_ALL | E_STRICT);
print_r(${'_SERVER'});
?>
<?php
error_reporting(E_ALL | E_STRICT);
$x = '_SERVER';
print_r($$x);
$_SERVER; // Touch anywhere in the script to avoid error
?>
I believe the interpreter checks if $_SERVER or $_ENV is called
somewhere in the
script. If it is not called anywhere, it just don't load the variable.
In my opinion, this behavior is wrong. Since PHP cannot know what I do
with
variables like $$x, it cannot know if I am actually trying to access
$_SERVER or
$_ENV .
Solutions:
(1) Always "enable" _SERVER and _ENV
(2) When solving $$x , check if it is _SERVER or _ENV which is still
disabled
and then enable it. Then, access it.
------------------------------------------------------------------------
[2011-03-02 04:52:11] daniel-marschall at viathinksoft dot de
Description:
------------
If you access $_ENV or $_SERVER the indirect way, via ${'_SERVER'} you
get an
E_NOTICE message. Please see my test scripts.
Test script:
---------------
Producing the bug:
<?php
error_reporting(E_ALL | E_STRICT);
$x = '_SERVER';
print_r($$x); // Notice: Undefined variable: _SERVER
?>
Workaround with dummy function which "touches" the variable:
<?php
error_reporting(E_ALL | E_STRICT);
function dummy($dummy) {}
dummy($_SERVER); // "Touch"
$x = '_SERVER';
print_r($$x); // Everything is OK!
?>
Expected result:
----------------
I don't expect an message that $_SERVER or $_ENV are not valid
variables. I kept
searching for hours because I couldn't find any reason, why $_SERVER
disappears,
but is visible in phpinfo(). I do not know why I have to "touch" these
super-
globals first, so that they get "active".
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54131&edit=1
Thread (4 messages)