For an experienced PHP programmer, it is no longer difficult to implement the string query function. Here we will introduce a shortcut to PHP string query for your reference. RL passing variables is a common thing for programmers, and many people will, because of... "> <LINKhref =" http://www.php100.com//statics/style/hea
For an experienced PHP programmer, it is no longer difficult to implement the string query function. Here we will introduce a shortcut to PHP string query for your reference.
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:
/* 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;
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.
$ 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 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.
$ Fields = array ('Paris ',
'New york ',
'Florence ',
'London ',
'Berlin ',
'Delhi ');
$ Url = "http :/
/Www. example. php? ".
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:
$ City_name = "new york ";
$ Invoice_id = 3456;
$ Currency_name = "euro ";
$ Total = 345;
$ Receipt_no = "fg44545 ";
$ Fields = array ('city' =>
$ City_name,
'Id' => $ invoice_id,
'Paid' => array ('currency '=>
$ Currency_name,
'Amount '=> $ total,
'Receip' => $ receipt_no)
);
$ Url = "http://www.example.php? ".
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.