Php obtains the names and values of all variables passed in the GET mode.

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.