Why would you merge $_GET and $_POST in PHP?

坚强是说给别人听的谎言 提交于 2019-12-10 12:43:36

问题


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);

回答1:


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 setting variables_order and as of PHP 5.3.0 can also be influenced by request_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 and request_order aren't configurable on a per script basis.

HTH




回答2:


$_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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/1419175/why-would-you-merge-get-and-post-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!