http_build_query
Builds a URL query string from an array.
Syntax
http_build_query(array|object $data): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $data | array|object | The 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=2A link to the next page:
<?php
$params = ['category' => 'drinks', 'page' => 3];
echo '/products?' . http_build_query($params);Output:
/products?category=drinks&page=3Notes
Do not build parameters by hand with string concatenation: it is easy to forget the encoding.
See also
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.