Rumah >pembangunan bahagian belakang >tutorial php >PHP查询字符串技巧分享_PHP教程
对于一个经验丰富的
RL传递变量对于程序员来说已经是司空见惯的事情,很多人会因此认为本文毫无新意。我们把通过URL传递变量的方式称作GET方式,另外一种是POST方式。这两种方式在PHP中都非常容易实现。举个例子,假设准备进行数据库查询,需要通过GET传递三个变量:city、id和paid。
传统的PHP查询字符串方法是象下面的例子那样构造查询字符串:
如今大部分PHP开发者都已经习惯于上面这种方法。它在只有三四个变量的时候毫无问题,但是再增加变量的话,代码
将变得难以理解和维护,并容易引入细微的错误。
传递GET变量的最佳方式是通过PHP5中引入的http_build_query函数,它接收一个数组参数,返回一个格式正确、经过
URL编码的字符串,可以直接拼接在url中。下面是相应的PHP查询字符串例子。
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">city_name</span><span> = </span><span class="attribute-value">"new york"</span><span>; </span></span></li> <li> <span>$</span><span class="attribute">invoice_id</span><span> = </span><span class="attribute-value">3456</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">paid</span><span> = </span><span class="attribute-value">1</span><span>; </span> </li> <li> <span>$</span><span class="attribute">fields</span><span> = </span><span class="attribute-value">array</span><span>('city' =</span><span class="tag">></span><span> <br>$city_name, </span> </li> <li class="alt"> <span>'id' =</span><span class="tag">></span><span> $invoice_id, </span> </li> <li> <span>'paid' =</span><span class="tag">></span><span> $paid); </span> </li> <li class="alt"> <span>$</span><span class="attribute">url</span><span> = </span><span class="attribute-value">"http://www.example.com?"</span><span> .<br> http_build_query($fields, '', "&"); </span> </li> </ol>
在上面这个PHP查询字符串例子中,数组包含了变量名和变量值。你也可以传入只含变量值的数组,函数会使用你提供的变量名(通
过函数的第二个参数传入)加上数组的索引值构造变量名。比如说你要传递六个城市名,可以象下面这么做。
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">fields</span><span> = </span><span class="attribute-value">array</span><span>('paris', </span></span></li> <li><span>'new york', </span></li> <li class="alt"><span>'florence', </span></li> <li><span>'london', </span></li> <li class="alt"><span>'berlin', </span></li> <li><span>'delhi'); </span></li> <li class="alt"> <span>$</span><span class="attribute">url</span><span> = </span><span class="attribute-value">"http:/<br>/www.example.php?"</span><span> . </span> </li> <li><span>http_build_query($fields,<br> 'city', "&"); </span></li> </ol>
产生的url如下:
http://www.example.php/?city0=paris&city1=new+york&city2=florence&city3=london&city4=berlin&city5=delhi
(译注:如果数组元素的key不是默认的整数,那么key就作为对应值的变量名,而象上面例子,数组的key是默认的整数,那么
变量名是函数第二个参数加上元素的key,所以第一个变量名就是city0)
PHP查询字符串函数的第三个参数是可选参数,表示变量的分隔符,默认值是‘&’。不过我更喜欢显式的传入这个‘&’分隔符。
此外还可以传入一个复杂的数组:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">city_name</span><span> = </span><span class="attribute-value">"new york"</span><span>; </span></span></li> <li> <span>$</span><span class="attribute">invoice_id</span><span> = </span><span class="attribute-value">3456</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">currency_name</span><span> = </span><span class="attribute-value">"euro"</span><span>; </span> </li> <li> <span>$</span><span class="attribute">total</span><span> = </span><span class="attribute-value">345</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">receipt_no</span><span> = </span><span class="attribute-value">"fgf44545"</span><span>; </span> </li> <li><span> </span></li> <li class="alt"> <span>$</span><span class="attribute">fields</span><span> = </span><span class="attribute-value">array</span><span>('city' =</span><span class="tag">></span><span> <br>$city_name, </span> </li> <li> <span>'id' =</span><span class="tag">></span><span> $invoice_id, </span> </li> <li class="alt"> <span>'paid' =</span><span class="tag">></span><span> array('currency' =</span><span class="tag">><br></span><span> $currency_name, </span> </li> <li> <span>'amount' =</span><span class="tag">></span><span> $total, </span> </li> <li class="alt"> <span>'receipt' =</span><span class="tag">></span><span> $receipt_no) </span> </li> <li><span>); </span></li> <li class="alt"> <span>$</span><span class="attribute">url</span><span> = </span><span class="attribute-value">"http://www.example.php?"</span><span> . </span> </li> <li><span>http_build_query($fields, '', "&"); </span></li> </ol>
它将生成以下URL:
http://www.example.com?city=new+york&id=3456&paid%5Bcurrency%5D=euro&paid%5Bamount%5D=345&paid%5Breceipt%
5D=fgf44545
总而言之,http_build_query()确实可以简化GET进行PHP查询字符串的构造。