PHP 8 reference · String Functions
PHP http_build_query() Function
Build a URL-encoded query string, such as page=2&sort=name, from an array. Nested arrays become key[]=... pairs.
PHP 8 Since 5.0
Syntax
http_build_query(array|object $data, string $numeric_prefix = "", ?string $arg_separator = null, int $encoding_type = PHP_QUERY_RFC1738): string
Parameters
| Parameter | Description |
|---|---|
data |
The parameters as an array or object. |
numeric_prefix optional |
A prefix for numeric keys. Default: "" |
arg_separator optional |
The separator between pairs. Default: "&" |
encoding_type optional |
PHP_QUERY_RFC1738 (spaces as +) or PHP_QUERY_RFC3986 (spaces as %20). Default: PHP_QUERY_RFC1738 |
Return value
The query string.
Examples
<?php
echo http_build_query(["q" => "tea & cake", "page" => 2]), "\n";
Output
q=tea+%26+cake&page=2
<?php
echo urldecode(http_build_query(["tags" => ["php", "web"], "filter" => ["min" => 5]])), "\n";
var_dump(http_build_query(["a" => null, "b" => true])); // null is left out
Output
tags[0]=php&tags[1]=web&filter[min]=5 string(3) "b=1"
Version history
| Version | Change |
|---|---|
| PHP 5.0 | Added |
In other languages
encodeURIComponent()· JavaScript
Related
parse_str()· Parse a query string such as a=1&b=2 into an array, the reverse of http_build_query(). The result argument is required.urlencode()· Encode a string for a URL query value: spaces become + and special characters become %XX codes.implode()· Join array elements into one string with a separator between them. join() is an alias of implode().- PHP Strings · tutorial