为什么要在 PHP 中合并 $_GET 和 $_POST ?
我刚刚在研究 WordPress 源代码(PHP)时看到了这段代码,您可以看到它们将所有 get 和 post 值合并/转换为 1 个请求数组。
现在据我所知, $_GET 和 $_POST 已经可以通过使用 array_merge() 函数调用 $_REQUEST WITHOUT 来使用,所以你知道为什么他们会这样做吗?
$_REQUEST = array_merge($_GET, $_POST);
I just saw this code while studying the wordpress source code (PHP), You can see they mergre/turn all get and post values into 1 request array.
Now as I know it, $_GET and $_POST are already available by calling $_REQUEST WITHOUT using the array_merge() function, so any ideas why they would do this?
$_REQUEST = array_merge($_GET, $_POST);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为默认的
$_REQUEST
是$_GET
、$_POST
AND$_COOKIE
的合并。此外,这些超全局变量合并到$_REQUEST
中的顺序取决于 ini 设置variables_order
,从 PHP 5.3.0 开始,还可能受到$_REQUEST
的影响。代码> request_order 。所以我的猜测是,开发人员希望确保
$_REQUEST
仅包含$_GET
和$_POST
,并按特定顺序合并,如果他无权访问 ini 设置(例如在共享主机上)。您会看到,variables_order
和request_order
无法针对每个脚本进行配置。华泰
That is because the default
$_REQUEST
is a merger of$_GET
,$_POST
AND$_COOKIE
. Also, the order in which the variables of these superglobals are merged into$_REQUEST
is dependant on the ini settingvariables_order
and as of PHP 5.3.0 can also be influenced byrequest_order
.So my guess is, that the developer wanted to make sure
$_REQUEST
consists of only$_GET
and$_POST
, merged in that particular order, if he didn't have access to ini settings (on a shared host for instance). You see,variables_order
andrequest_order
aren't configurable on a per script basis.HTH
$_REQUEST
默认包含$_GET
、$_POST
和$_COOKIE
数组的内容。也许他们想从中排除 COOKIE 变量,因为它通常不用于此目的。$_REQUEST
contains the contents of$_GET
,$_POST
, and$_COOKIE
arrays by default. Maybe they want to exclude COOKIE variables from it, since it is generally not used for that purpose.因此,如果您有一个同名的 GET 变量和一个 POST 变量,它将选择 POST 变量而不是 GET 变量。
另外,您可能不希望 $_REQUEST 变量中包含 cookie。
This is so if you have a GET variable and a POST variable with the same name, it will choose the POST variable over the GET one.
Also you may not want the cookies in the $_REQUEST variable.
我不知道具体为什么要在您看到的地方完成它,但我之前已经看到过这样做,当时对一个数组或另一个数组中的值进行了一些处理,并且您希望将这些更改合并回 $_REQUEST 以便任何人使用 $_REQUEST 将获得更改,即使这些更改是对 $_POST 或 $_GET 变量进行的。
这种情况会出现在像 Wordpress 这样的情况下,因为插件开发人员可能会使用这些变量中的任何一个来访问数据,而 Wordpress 核心需要确保它们都获得相同的数据。
为什么你不想直接对 $_REQUEST 执行此操作?因为 $_REQUEST 包含大量 $_POST 和 $_GET 没有的额外信息。您可能不想将处理应用于所有这些额外的位。
I don't know specifically why it was done where you saw it, but I have seen that done before when some processing has been done on the values in one array or another and you want to merge those changes back into $_REQUEST so that anyone using $_REQUEST will get the changes even though they were done to the $_POST or $_GET variables.
This comes up in situations like Wordpress has because plugin developers could be using any of those variables to access data and the Wordpress core would need to make sure they all get the same data.
Why wouldn't you want to do it to $_REQUEST directly? Because $_REQUEST contains a ton of extra info that $_POST and $_GET don't have. You might not want to apply your processing to all those extra bits.