PHP Query string tips sharing

Source: Internet
Author: User
Tags array implement integer variables variable

For an experienced PHP programmer, it is no longer a difficult task to implement the query function of a string. Here we will introduce a PHP query string shortcut techniques for your reference.

RL transfer variables are commonplace for programmers, and many people think this article is nothing new. The way we pass a variable through a URL is called a Get method, and the other is the Post method. Both of these methods are very easy to implement in PHP. For example, suppose you are ready to make a database query, you need to pass three variables through get: City, ID, and paid.

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

/* Assume we want to pass this
Variables *
$city _name = "New York";
$invoice _id = 3456;
$paid = 1;
$query _string = "city={$city _name}
&id={$invoice _id}&paid={$paid} ";
$url = "http://www.example.com?".
$query _string;

Most PHP developers today are accustomed to this approach. It has no problem with only three or four variables, but if you add a variable, the code

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

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

A URL-encoded string that can be spliced directly in the URL. The following is an example of the corresponding PHP query string.

$city _name = "New York";
$invoice _id = 3456;
$paid = 1;
$fields = Array (' City ' =>
$city _name,
' id ' => $invoice _id,
' Paid ' => $paid);
$url = "http://www.example.com?".
Http_build_query ($fields, "&");

In this example of the PHP query string above, the array contains variable names and variable values. You can also pass in an array containing only variable values, and the function will use the variable name you provide (pass

The second parameter of the function is passed in) plus the index value of the array to construct the variable name. For example, you have to pass six city names, and you can do so as follows.

$fields = Array (' Paris '),
' New York ',
' Florence ',
' London ',
' Berlin ',
' Delhi ');
$url = "http:/
/www.example.php? ".
Http_build_query ($fields,
' City ', ' & ');

The resulting 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 the array element is not the default integer, then key is the variable name of the corresponding value, and as the example above, the key of the array 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 argument that represents the delimiter for the variable, and the default value is ' & '. But I prefer to explicitly pass this ' & ' separator.

You can also pass in a complex array:

$city _name = "New York";
$invoice _id = 3456;
$currency _name = "Euro";
$total = 345;
$receipt _no = "fgf44545";

$fields = Array (' City ' =>
$city _name,
' id ' => $invoice _id,
' Paid ' => array (' Currency ' =>
$currency _name,
' Amount ' => $total,
' Receipt ' => $receipt _no)
);
$url = "http://www.example.php?".
Http_build_query ($fields, "&");

It will generate the following URL:

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

5d=fgf44545

In summary, Http_build_query () does simplify the construction of a get for a PHP query string.







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.