views:

203

answers:

4

Consider the following snippets of code:

Exhibit A:

$_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true);

Exhibit B:

${'_REQUEST'} = json_decode(stripslashes(json_encode(${'_REQUEST'}, JSON_HEX_APOS)), true);

Exhibit C:

${'_' . 'REQUEST'} = json_decode(stripslashes(json_encode(${'_' . 'REQUEST'}, JSON_HEX_APOS)), true);

Both exhibit A and B work perfectly fine, exhibit C however displays a very strange error message:

Notice: Undefined variable: _REQUEST

What makes it even more weird is that this only happens with the $_REQUEST superglobal, if I try it with $_GET, $_POST or $_COOKIE all experiments work fine without raising error notices.

I'm guessing this is a PHP bug? I'm running on PHP 5.3.0.

A: 

I'm going with the bug since $_GET etc do work It's not been mentioned at the php bugtrack: http://bugs.php.net/

Maybe you should report it.


I did some quick debugging in Zend Studio:

<?php
var_dump( ${'_' . 'REQUEST'});
var_dump( ${'_REQUEST'});
var_dump( $_REQUEST);

And it seemed to work with the included PHP 5.2.10. Maybe you could use this as a workaround:

$foo = '_' . 'REQUEST'
$$foo //<-- is the same as $_REQUEST

EDIT: Woops, this wouldn't work with Superglobals, sorry -- thanks Cacha102

The Guy Of Doom
+5  A: 

(I tested with PHP 5.3.1)

One funny thing is that this portion of code :

<?php
var_dump(${'_' . 'REQUEST'});

Gets the notice Undefined variable: _REQUEST


But this one :

<?php
var_dump($_REQUEST);
var_dump(${'_' . 'REQUEST'});

Doesn't give any notice, and shows two empty arrays.


For a while, I though this could be related to auto_globals_jit, but $_REQUEST doesn't seem to the concerned by that directive... But there is one interested thing said, here :

Usage of SERVER and ENV variables is checked during the compile time so using them through e.g. variable variables will not cause their initialization.

Maybe, after all, even if it's not said in the manual, auto_globals_jit has an impact on $_REQUEST...


And, to be sure, I turned Off auto_globals_jit in my php.ini file :

; When enabled, the SERVER and ENV variables are created when they're first
; used (Just In Time) instead of when the script starts. If these variables
; are not used within a script, having this directive on will result in a
; performance gain. The PHP directives register_globals, register_long_arrays,
; and register_argc_argv must be disabled for this directive to have any affect.
; http://www.php.net/manual/en/ini.core.php#ini.auto-globals-jit
auto_globals_jit = Off

And tried this code again :

<?php

var_dump(${'_' . 'REQUEST'});

And I now get an empty array, and not a notice anymore.

So it seems auto_globals_jit does indeed have an impact on $_REQUEST -- even if it's not mentionned in the manual.

Pascal MARTIN
Maybe it is possible that the `$_REQUEST` global is never created unless you access it directly, and concatenating the strings together doesn't trigger its creation. If so, would be really bad programming.
Chacha102
On that page you linked, it states that you can't use variable variables with superglobal arrays. probably caused by that line you just gave us.
Chacha102
@Chacha102 : I edited my post at about the same time you posted your comment, to copy-paste a note about that (and I just saw you posted another answer that quotes another section of the manual that says the same kind of thing) -- this comment arrived after your second one, but was posted before I saw it, btw ^^
Pascal MARTIN
kk. I just deleted mine since it seems like you've figured it out :)
Chacha102
@Chacha102 : your answer included the quote from the "variables variable", that was interesting too -- and which I didn't quote
Pascal MARTIN
@Pascal: Thanks, can you reproduce the notice with other superglobals and `auto_globals_jit = On`?
Alix Axel
@Alix : I have exactly the same behavior (i.e notice when `auto_globals_jit=On` and no notice when `auto_globals_jit=Off`) with `SERVER` ; but not with `GET` -- I suppose this relates to the sections of the manual @Chacha102 and I quoted
Pascal MARTIN
@Pascal: Got it, thanks! Still it's confusing since the manual says that superglobals can't be used as variable variables...
Alix Axel
@Alix : now that you're saying it, it's strange indeed ^^ Not sure why/how I don't get the notice with GET ; maybe GET/POST are more "magic" than SERVER :-D (I suppose GET is always created, no matter what -- but not sure about that)
Pascal MARTIN
+1  A: 

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.

http://www.php.net/manual/en/language.variables.variable.php

This is most likely related to:

Usage of SERVER and ENV variables is checked during the compile time so using them through e.g. variable variables will not cause their initialization.

From Pascal's Answer.

Which can all be related back to the auto_globals_jit option.

Chacha102
A: 

Found this "bug" report. According to tony2001@php it's not a bug:

Variable variables: Superglobals cannot be used as variable variables inside functions or class methods.

This page says the same thing, but what is weird is that this only happens with $_REQUEST, other GPC superglobals don't raise the error notice, can anyone double-check this?

Alix Axel