http_build_query()

http_build_query() — Generate URL-encoded query string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Generates a URL-encoded query string from the associative (or indexed) array provided.

Signature

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

Parameters

ParameterTypeWhat it takes
$dataarray|objectMay be an array or object containing properties.
$numeric_prefixstringIf numeric indices are used in the base array and this parameter is provided, it will be prepended to the numeric index for elements in the base array only.
$arg_separator?stringThe argument separator. If not set or null, arg_separator.output is used to separate arguments.
$encoding_typeintBy default, PHP_QUERY_RFC1738.

Return value

Returns a URL-encoded string.

Example

A runnable snippet:

$data = array( 'foo' => 'bar', 'baz' => 'boom', 'cow' => 'milk', 'null' => null, 'php' => 'hypertext processor' ); echo http_build_query($data) . "\n"; echo http_build_query($data, '', '&');

Output:

foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&baz=boom&cow=milk&php=hypertext+processor

Changelog

VersionChange
8.4.0Prior to PHP 8.4.0, BackedEnum properties of data were converted to objects, rather than their scalar equivalents.
8.0.0arg_separator is now nullable.

See also

parse_str(), parse_url(), urlencode(), array_walk()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page http_build_query() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.