Home php教程 php手册 PHP查询字符串技巧分享

PHP查询字符串技巧分享

Jun 13, 2016 am 11:11 AM
php transfer share variable commonplace string Skill yes Inquire of programmer

对于一个经验丰富的

RL传递变量对于程序员来说已经是司空见惯的事情,很多人会因此认为本文毫无新意。我们把通过URL传递变量的方式称作GET方式,另外一种是POST方式。这两种方式在PHP中都非常容易实现。举个例子,假设准备进行数据库查询,需要通过GET传递三个变量:city、id和paid。

传统的PHP查询字符串方法是象下面的例子那样构造查询字符串:

  1. /* assume we want to pass this 
    variables */  
  2. $city_name = "new york";  
  3. $invoice_id = 3456;  
  4. $paid = 1;  
  5. $query_string = "city={$city_name}
    &id={$invoice_id}&paid={$paid}"
    ;  
  6. $url = "http://www.example.com?" . 
    $query_string; 

如今大部分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>
Copy after login

在上面这个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>
Copy after login

产生的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>
Copy after login

它将生成以下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查询字符串的构造。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles