PHP query string tips sharing _php Tutorial

Source: Internet
Author: User
For an experienced

The RL pass-through variable is already commonplace for programmers, and many people will think this article is nothing new. We refer to the way the variable is passed through the URL called the Get method, and the other is the Post method. Both of these approaches are very easy to implement in PHP. For example, suppose you are preparing a database query, you need to pass three variables via get: City, ID, and paid.

The traditional PHP query string method constructs a query string as in the following example:

    1. /* Assume we want to pass this
      Variables * *
    2. $ City_name = "New York" ;
    3. $ invoice_id = 3456 ;
    4. $ paid = 1 ;
    5. $ query_string = "city={$city _name}
      &id={$invoice _id}&paid={$paid} ";
    6. $ URL = "http://www.example.com?" .
      $query _string;

Most PHP developers are now accustomed to this approach. It has no problem when there are only three or four variables, but if you add variables, the code

will become difficult to understand and maintain, and easily introduce subtle errors.

The best way to pass a get variable is through the Http_build_query function introduced in PHP5, which takes an array parameter and returns a properly formatted,

URL-encoded string that can be stitched directly into the URL. Here is an example of the corresponding PHP query string.

 
 
  1. $ City_name = "New York" ;
  2. $ invoice_id = 3456 ;
  3. $ paid = 1 ;
  4. $ Fields = Array (' City ' =>
    $city _name,
  5. ' id ' = > $invoice _id,
  6. ' paid ' = > $paid);
  7. $ URL = "http://www.example.com?" .
    Http_build_query ($fields, "," & ");

In the above example of a PHP query string, the array contains the variable name and the value of the variable. You can also pass in an array that contains only variable values, and the function uses the variable name you provide

The second parameter of the passed function is passed in) plus the index value of the array constructs the variable name. Say you want to pass six city names, you can do as below.

 
  
  
  1. $ Fields Array(' Paris ',
  2. ' New York ',
  3. ' Florence ',
  4. ' London ',
  5. ' Berlin ',
  6. ' Delhi ');
  7. $ URL "http:/
    /www.example.php? " .
  8. Http_build_query ($fields,

The resulting URLs are as follows:

Http://www.example.php/?city0=paris&city1=new+york&city2=florence&city3=london&city4=berlin &city5=delhi

If the array element's key is not the default integer, then the key is the variable name of the corresponding value, and as the example above, the array key is the default integer, then

The variable name is the second parameter of the function plus the key of the element, so the first variable name is CITY0)

The third parameter of the PHP query string function is an optional parameter that represents the delimiter of the variable, and the default value is ' & '. But I prefer to explicitly pass in the ' & ' delimiter.

You can also pass in a complex array:

 
 
  1. $ City_name = "New York" ;
  2. $ invoice_id = 3456 ;
  3. $ Currency_name = "Euro" ;
  4. $ Total = 345 ;
  5. $ Receipt_no = "fgf44545" ;
  6. $ Fields = Array (' City ' =>
    $city _name,
  7. ' id ' = > $invoice _id,
  8. ' paid ' = > Array (' currency ' =>
    $currency _name,
  9. ' Amount ' = > $total,
  10. ' Receipt ' = > $receipt _no)
  11. );
  12. $ URL = "http://www.example.php?" .
  13. Http_build_query ($fields, "," & ");

It will generate the following URLs:

Http://www.example.com?city=new+york&id=3456&paid%5Bcurrency%5D=euro&paid%5Bamount%5D=345&paid %5breceipt%

5d=fgf44545

All in all, http_build_query () does make it easy to construct a get for PHP query strings.


http://www.bkjia.com/PHPjc/445934.html www.bkjia.com true http://www.bkjia.com/PHPjc/445934.html techarticle for an experienced RL pass-through variable is a common thing for programmers, many people will think this article is nothing new. We pass the URL by the way the variable is called ...

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