PHP obtains the names and values of all variables passed in the get method.

Source: Internet
Author: User
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 it before you do it. I just didn't Google it, causing 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 ideas most people encounter this kind of requirement. The first response should be the for loop get array. Join the strings yourself and write a code to share the 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;

Although the results are the same, the efficiency of the For Loop is certainly low when there are many get parameters. Therefore, when writing code using PHP, 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.