PHP Query string Tips

Source: Internet
Author: User
Tags implement

For an experienced PHP programmer, it is no longer a difficult task to implement the query function of a string. Here we will introduce a PHP query string shortcut techniques for your reference.

A deep understanding of PHP plug-in mechanism principle

Explore PHP variable parsing order How to get submit data

A thorough understanding of PHP operating mechanism

Simple analysis of PHP function extract () application technique

Summarize some PHP information functions for you

RL transfer variables are commonplace for programmers, and many people think this article is nothing new. The way we pass a variable through a URL is called a Get method, and the other is the Post method. Both of these methods are very easy to implement in PHP. For example, suppose you are ready to make a database query, you need to pass three variables through get: City, ID, and paid.

The traditional PHP query string method constructs the 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  .
   
$query _string;

Most PHP developers today are accustomed to this approach. It has no problem with only three or four variables, but when you add variables, the code becomes difficult to understand and maintain, and it's easy to introduce subtle errors.

The best way to pass a get variable is through the Http_build_query function introduced in PHP5, which receives an array parameter, returns a properly formatted, URL-encoded string that can be spliced directly into the URL. The following 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_build _query ($fields, "&");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.