Php obtains the names and values of all variables passed in the GET method. preface this blog requires that an http request such as 127.0.0.1? A = 123 & amp; B = 456 & amp; c = 789 all get parameters are obtained and concatenated to the end of test.com, that is, the final ideal uri should be the name and value of all variables passed in the GET method obtained by test php.
Preface the requirement of this blog is that I need to send an http request such as 127.0.0.1? A = 123 & B = 456 & c = 789 obtain all the get parameters and concatenate them to the end of test.com. that is, the final ideal uri should be test.com? A = 123 & B = 456 & c = 789 can be implemented in two ways. we recommend that you google before doing so, and I just did not google to cause rework.
$ _ SERVER ["QUERY_STRING"]
This is the simplest method, but most people may not be familiar with this server variable.
$ _ SERVER ["QUERY_STRING"]: query string
Code
$base = "test.com";$str = $_SERVER["QUERY_STRING"];$uri = $base.$str;echo $uri;
Effect
$ _ GET array for loop concatenation
The first response to this kind of requirement is the for loop GET array. join the strings and write a shared code.
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
Although the results are the same, the efficiency of the for loop is certainly low when there are many get parameters. Therefore, when using php to write code, try to make fewer wheels, so I prefer c, so you can move your mind more. Alas, php is a silly language, but it is really convenient!