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:
- /* 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 = "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 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...