超全局 $_GET 中键值对的顺序是否得到保证?
$_GET 超全局变量中键值对的顺序是否保证与请求 URL 中接收字段值对的方式一致?
例如,给定 Web 服务器收到的 URL 请求:
index.php?a=1&foo=bar&b=2
...以及此代码:
foreach ($_GET as $key => $value)
{
echo $key . ": " . $value\n";
}
...结果将始终保证为:
a: 1
foo: bar
b: 2
我没有在 $_GET 的 PHP 文档中看到任何关于键排序的提及或一般而言,超全局变量。这让我相信键值对的顺序是不可靠的。
有谁知道订单是否保证一致性,或者更好地指出规范/文档来澄清这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好假设它们不可靠有两个原因。首先,这没有记录。未记录的内容如有更改,恕不另行通知...因为如果他们从未第一次通知您,为什么现在需要这样做?其次,你不能 100% 确信客户不会以某种方式修改数据。
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.
是的,会的。您可以在 main/php_variables.c 中检查源代码,函数“SAPI_TREAT_DATA_FUNC”。有一个简单的循环,它按顺序从查询字符串中读取变量,然后如果它们通过过滤器,则将它们添加到超全局数组(按顺序,使用 php_register_variable_ex())。
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.
在 PHP 中,数组是一个有序映射,而不是一个简单的散列。这意味着,订单是有保证的,是的。
编辑:打字错误
In PHP arrays are an ordered map, not a simple hash. This means, the order is guaranteed, yes.
Edit: Typos