Question

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);
Was it helpful?

Solution

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

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top