3

Is the order of the keys-value pairs in the $_GET superglobal variable guaranteed to be consistent with how the field-value pairs were received in the requested URL?

For example, given this URL request received by the web server:

index.php?a=1&foo=bar&b=2

...and this code:

foreach ($_GET as $key => $value)
{
    echo $key . ": " . $value\n";
}

...will the result always be guaranteed to be:

a: 1
foo: bar
b: 2

I didn't see any mention of key ordering in the PHP doc of $_GET or superglobals in general. This leads me to believe that the order of the key-value pairs cannot be relied upon.

Does anyone know if the order has a guaranteed consistency to it, or better yet point to spec/doc that clarifies this?

Robert Groves
  • 7,574
  • 6
  • 38
  • 50
  • 1
    I dont see whay it wouldnt be unless youre not actually passing in the query string youre expecting. That said whay yould would you ever rely on the order instead of accessing them directly by name? – prodigitalson Jul 22 '11 at 06:28

3 Answers3

3

It is best to assume that they are not reliable for two reasons. First, this is not documented. Things which are not documented are subject to change without notice... because if they never notified you the first time, why do the need to now? Second, you can't be 100% positive that the client won't somehow mung the data.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
1

Yes, it will. You can check source code at main/php_variables.c, function "SAPI_TREAT_DATA_FUNC". There is a simple loop that reads variables from query string in order, then adds them to superglobals array (in order, with php_register_variable_ex()) if they pass filter.

XzKto
  • 2,472
  • 18
  • 18
0

In PHP arrays are an ordered map, not a simple hash. This means, the order is guaranteed, yes.

Edit: Typos

ckruse
  • 9,642
  • 1
  • 25
  • 25