PHP gets all variable names and values passed in Get mode

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

  • 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.