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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

http_build_query(array|object $data, string $numeric_prefix = "", ?string $arg_separator = null, int $encoding_type = PHP_QUERY_RFC1738): string

Parameters

ParameterDescription
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

Build a query stringTry it yourself
<?php
echo http_build_query(["q" => "tea & cake", "page" => 2]), "\n";

Output

q=tea+%26+cake&page=2
Arrays, null and booleansTry it yourself
<?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

VersionChange
PHP 5.0Added

In other languages

Was this page helpful?