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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles