Home  >  Article  >  Backend Development  >  PHP function introduction—http_build_query()`: Build URL query string

PHP function introduction—http_build_query()`: Build URL query string

王林
王林Original
2023-07-26 12:57:204574browse

PHP function introduction—http_build_query(): Build URL query string

In PHP development, you often encounter scenarios where you need to combine some parameters into a URL query string. In order to facilitate developers to process these parameters and convert them into strings that comply with URL specifications, PHP provides the http_build_query() function.

Function introduction
http_build_query() function is a very practical function. Its function is to convert an associative array or object into a URL query string. This function can automatically convert the key-value pairs of an array or object into a string that conforms to the URL specification and connect them with the "&" symbol.

Code Example
The following is a code example that shows how to use the http_build_query() function:

<?php
$params = [
    'name' => 'John',
    'age' => 25,
    'city' => 'New York'
];

$queryString = http_build_query($params);
echo $queryString;
?>

The output is:

name=John&age=25&city=New+York

In the above example, We define an associative array $params, which contains three parameters: name, age and city. We then use the http_build_query() function to convert the $params array into a URL query string and assign the result to the $queryString variable. Finally, we use the echo statement to output the value of $queryString.

Parameter processing
http_build_query() function can also handle complex parameter structures. For example, it can handle multidimensional arrays, objects, and special characters.

Multidimensional array
If the $params array is a multidimensional array, the http_build_query() function will automatically convert it into a string that conforms to the URL specification.

<?php
$params = [
    'name' => 'John',
    'age' => 25,
    'city' => 'New York',
    'hobbies' => [
        'reading',
        'swimming',
        'traveling'
    ]
];

$queryString = http_build_query($params);
echo $queryString;
?>

The output result is:

name=John&age=25&city=New+York&hobbies%5B0%5D=reading&hobbies%5B1%5D=swimming&hobbies%5B2%5D=traveling

In the above example, we added an element named hobbies to the $params array, whose value is an array containing three hobbies. When we use the http_build_query() function to convert the $params array into a URL query string, we will find that the hobbies parameter is converted into the form of hobbies=reading&hobbies[1]=swimming&hobbies[2]=traveling.

Object
Similarly, the http_build_query() function can also handle objects. The function automatically converts the object's public property key-value pairs into a URL query string.

<?php
class Person {
    public $name = 'John';
    public $age = 25;
    public $city = 'New York';
}

$person = new Person();
$queryString = http_build_query($person);
echo $queryString;
?>

The output result is:

name=John&age=25&city=New+York

In the above example, we define a class named Person and define three public properties in the class: name, age and city . We then created a Person object and passed it to the http_build_query() function, which converted the object's properties into a URL query string.

Special characters
When the parameters contain special characters, the http_build_query() function will automatically escape them to comply with URL specifications.

<?php
$params = [
    'name' => 'John Doe',
    'age' => 25,
    'city' => 'New York'
];

$queryString = http_build_query($params);
echo $queryString;
?>

The output is:

name=John+Doe&age=25&city=New+York

In the above example, our name parameter contains spaces. When using the http_build_query() function to convert the $params array into a URL query string, the function converts spaces into plus signs.

Summary
http_build_query() function is a very practical function in PHP. It can help developers quickly and easily convert an associative array or object into a URL query string. Whether it is a simple parameter list or a complex multi-dimensional array or object, the http_build_query() function can handle it. By properly using the http_build_query() function, we can process URL parameters more efficiently and improve the readability and maintainability of the code.

The above is the detailed content of PHP function introduction—http_build_query()`: Build URL query string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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