views:

418

answers:

4

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);
+2  A: 

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.

Henri Watson
Actually, it will chose the $_POST over the $_GET. array_merge manual: http://www.php.net/manual/en/function.array-merge.php
Havenard
"[...] it will choose the GET variable over the POST one". That's not true; it is the other way around. Values of latter arrays with corresponding keys will overwrite former values. See http://www.php.net/array_merge
fireeyedboy
Fixed information.
Henri Watson
A: 

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.

Gabriel Hurley
+2  A: 

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

Fragsworth
+1 Just writing the same thing.
Havenard
ya, perhaps for security reasons, they want to eliminate COOKIES from $_REQUEST, get able to use GET and POST together. +1 for the great minds thinking alike.
thephpdeveloper
+5  A: 

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

fireeyedboy