Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

http_build_query

Builds a URL query string from an array.

Syntax

http_build_query(array|object $data): string

Parameters

ParameterTypeDescription
$dataarray|objectThe parameters as a "name => value" array.

Return value

stringThe query string; the values are already encoded.

Examples

Parameters from an array:

<?php
 
echo http_build_query(['q' => 'tea & coffee', 'page' => 2]);

Output:

q=tea+%26+coffee&page=2

A link to the next page:

<?php
 
$params = ['category' => 'drinks', 'page' => 3];
 
echo '/products?' . http_build_query($params);

Output:

/products?category=drinks&page=3

Notes

Do not build parameters by hand with string concatenation: it is easy to forget the encoding.

See also

  • urlencode — Encodes a string for use in a URL.
  • parse_url — Splits a URL into its parts.

Related recipes

In the tutorial

  • HTTP requests and responses — How PHP receives a request and builds a response — the request method and address, headers, status codes, redirects and JSON responses.

Thanks for reading!

Donate to the site — we work hard for you.