Skip to content

Core\Uri::buildQuery

Writes an array of names and values as a query string, the part of a link after the ?.

Core\Uri::buildQuery(["q" => "red shoes", "page" => 2]) returns q=red+shoes&page=2. Each name and value is escaped, so a space becomes + and a character such as & or = cannot start a new pair. A value can be a list or another array. Its items are written under their whole path, such as sizes[0]=M. true is written as 1 and false as 0. A null value is left out. A date or time, such as a Core\Time\Date or an Instant, is written as its ISO text, and its + becomes %2B. A class that implements Parses is written as the text its format method returns.

Core\Uri::parseQuery reads the text back to the same names and values.

The examples below build a search link, write lists and nested values, build the link to the next page of a list, and write dates and times.

Parameters

  • $parametersarray<mixed>The names and values to write. A value is a scalar or another array. A value in a nested array is written under its whole path, such as filter[size][0]=M. The brackets are escaped as %5B and %5D. A value can also be a Core\Time\Date, TimeOfDay, Instant or DateTime. It can also be an object of a class that implements Parses, such as a Core\Uuid.

Return value

Returns string. The query string, without a leading ?. Names and values are escaped as Core\Uri::encodeFormValue escapes them, so a space is +. true is written as 1 and false as 0. A time value is written as its toIso text, so a + in it is %2B. A Parses object is written as the text its format method returns. A null value writes nothing, and neither does an empty array.

Errors

  • RuntimeErrorA value is a function, or an object that is not a time value and does not implement Parses, so it has no text form.

Changelog

VersionChange
0.0.6Core\Router::url and Core\Uri::buildQuery write a Core\Time value as its ISO text, and a Parses object as the text of its format method. Core\Router::url leaves out a query key whose value is null. A value in the $params array whose type the parameter cannot take, such as a Date for a uint, does not compile.

Examples

A search link

<?nvs
// Shows how to build the query string of a link from an array.
array<mixed> $search = ['q' => 'red shoes', 'page' => 2];
// A space in a value is written as `+`. This prints /search?q=red+shoes&page=2.
echo '/search?', Core\Uri::buildQuery($search), "\n";
// A character with a meaning in a query string, such as `&` or `=`, is escaped.
// This prints q=salt+%26+pepper.
echo Core\Uri::buildQuery(['q' => 'salt & pepper']), "\n";
Output
/search?q=red+shoes&page=2
q=salt+%26+pepper

Lists and nested values

<?nvs
// Shows how lists, nested arrays, `bool` and `null` values are written.
array<mixed> $filter = [
'sizes' => ['M', 'L'],
'price' => ['min' => 10, 'max' => 50],
'inStock' => true,
'coupon' => null,
];
// A nested value is written under its whole path, such as `sizes[0]`, with escaped brackets.
// `true` is written as `1`, and the `null` value writes nothing.
echo Core\Uri::buildQuery($filter), "\n";
// `Core\Uri::parseQuery` reads the text back to the same names and values.
// This prints L and 50.
array<mixed> $read = Core\Uri::parseQuery(Core\Uri::buildQuery($filter));
echo ($read['sizes'] as array<mixed>)[1] as bytes as string, "\n";
echo ($read['price'] as array<mixed>)['max'] as bytes as string, "\n";
Output
sizes%5B0%5D=M&sizes%5B1%5D=L&price%5Bmin%5D=10&price%5Bmax%5D=50&inStock=1
L
50

A link to the next page

<?nvs
// Shows how to build the link to the next page of a list, and keep the filters the visitor chose.
// In a web request, this text comes from the address of the current page.
string $current = 'category=shoes&sort=price&page=2';
array<mixed> $parameters = Core\Uri::parseQuery($current);
int $page = ($parameters['page'] as bytes as string) as int;
// Only `page` changes. The other names and values stay as they were.
$parameters['page'] = $page + 1;
// This prints /products?category=shoes&sort=price&page=3.
echo '/products?', Core\Uri::buildQuery($parameters), "\n";
Output
/products?category=shoes&sort=price&page=3

Dates and times

<?nvs
// Shows how a date and a moment in time are written into a query string.
array<mixed> $range = [
'from' => Core\Time\Date::fromIso('2026-01-05'),
'until' => Core\Time\Instant::fromIso('2026-01-31T18:00:00Z'),
];
// Each value is written as its ISO text, and `:` becomes `%3A`.
// This prints from=2026-01-05&until=2026-01-31T18%3A00%3A00Z.
echo Core\Uri::buildQuery($range), "\n";
// A `DateTime` has an offset and a zone. The `+` becomes `%2B`, so it is not read as a space.
var $meeting = Core\Time\DateTime::fromIso('2026-01-05T14:00:00+01:00[Europe/Vienna]');
echo Core\Uri::buildQuery(['meeting' => $meeting]), "\n";
Output
from=2026-01-05&until=2026-01-31T18%3A00%3A00Z
meeting=2026-01-05T14%3A00%3A00%2B01%3A00%5BEurope%2FVienna%5D