PHP gets all the variable names and values passed in by the Get method
Objective
The requirement of this blog is that I need an HTTP request such as 127.0.0.1?a=123&b=456&c=789 to take all the get parameters out to the back of the test.com, that is, the final ideal URI should be test.com?a= 123&b=456&c=789
Two methods can be implemented, recommended to do before Google, I just don't have Google cause rework
$_server["Query_string"]
Brief introduction
This is the simplest approach, but most people may not be too familiar with this server variable, explaining
$_server["Query_string"]: query string
Code
$base = "test.com"; $str = $_server["Query_string"]; $uri = $base. $str; Echo $uri;
Effect
$_get array for loop string
Ideas
Most people meet this demand the first reaction should be a for loop get array, self-puzzle, write a implementation code share
Code
$str = "test.com?"; $count = count ($_get), $i = 0;foreach ($_get as $key + = $value) { if ($i = = $count-1) { $str. = $key. "=" . $value; } else { $str. = $key. "=" . $value. "&"; } $i + +;} Echo $str;
Effect
Comparison
Although the effect is the same, but the get parameters for a long time, the efficiency of the For loop is certainly low, so use PHP to write code as little as possible to build their own wheels, so I prefer C, so can be more brain, alas, PHP is a fool's language, but it is indeed convenient!