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:
- /* 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 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.
- $ 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 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.
- $ Fields Array(' Paris ',
- ' New York ',
- ' Florence ',
- ' London ',
- ' Berlin ',
- ' Delhi ');
- $ URL "http:/
/www.example.php? " .
- 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:
- $ 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 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 ...