PHP string query tips _ PHP Tutorial

Source: Internet
Author: User
PHP string query tips. It is common for programmers to pass variables in an experienced RL program. many people think this article is not new. We refer to the method of passing variables through URLs as an experienced

It is common for programmers to pass RL variables. many people think that this article is not new. We call the method of passing variables through URLs as the GET method, and the other method is the POST method. Both methods are very easy to implement in PHP. For example, if you want to query a database, you need to pass three variables through GET: city, id, and paid.

The traditional PHP string query method is to construct a query string as shown 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;

Nowadays, most PHP developers are used to the above method. It is no problem when there are only three or four variables, but when the variable is added, the code

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

The best way to pass the GET variable is through the http_build_query function introduced in PHP5. it receives an array parameter and returns a correct format.

A url-encoded string that can be directly spliced into a url. The following is an example of a 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 PHP query string example, the array contains the variable name and variable value. You can also input an array containing only variable values. the function will use the variable name you provided (

Input the second parameter of the function) and the index value of the array to construct the variable name. For example, if you want to pass six city names, you can do the following.

 
 
  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,
    'city', "&");

The generated url is as follows:

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

If the key of an array element is not the default integer, the key is used as the variable name of the corresponding value. in the preceding example, the key of an array is the default integer.

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 used by PHP to query string functions is an optional parameter, indicating the variable Separator. the default value is '&'. However, I prefer to explicitly input the '&' separator.

You can also input 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 generates the following URL:

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

5D = fg44545

All in all, http_build_query () can simplify the PHP query string construction by GET.


Variable passing through the explain RL is a common practice for programmers. many people think that this article is not new. We will pass the variable name through URL...

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.