Skip to content

Core\Uri::withQueryParameter

$uri->withQueryParameter(string $name, mixed $value): Core\Uri$uri->withQueryParameter(
    string $name,
    mixed $value,
): Core\Uri

Returns a new Uri with one query parameter set to a new value. For /search?q=shoes&page=2, withQueryParameter("page", 3) returns /search?q=shoes&page=3. The Uri you call it on does not change.

When the name is not in the query yet, the pair is added at the end. The value is encoded for you, so a space becomes + and & becomes %26. An array value is written with brackets, the same way Core\Uri::buildQuery writes it.

A null value removes the parameter. When you remove the last parameter, the address has no ? at all.

Good to know: the whole query is written again. The other pairs keep their values, but the way they are encoded can change. For example, %20 becomes +.

The examples below change a page number, remove a parameter, and build the links of a page list.

Parameters

  • $namestringThe name of the parameter, without brackets. An array value writes the brackets.
  • $valuemixedThe new value. It can be a scalar or an array. An array is written with brackets, the same way Core\Uri::buildQuery writes it. null removes the parameter.

Return value

Returns Core\Uri. A new Uri. When the last parameter is removed, the address has no ?. The whole query is written again, so the encoding of the other pairs can change: %20 becomes +.

Errors

  • RuntimeErrorA name in the query has escapes that do not decode to valid UTF-8 text. It is also thrown when value is not a scalar, an array or null, for example an object.

Examples

Change the page number

<?nvs
// Shows how to change one value in the query of an address.
var $link = Core\Uri::parse('https://shop.example.com/search?q=red+shoes&page=2');
// The value of `page` is replaced. This prints the address with `page=3`.
echo $link->withQueryParameter('page', 3), "\n";
// A name that is not in the query is added at the end. The space becomes `+`.
echo $link->withQueryParameter('sort', 'price low'), "\n";
// `$link` does not change. This prints the first address again.
echo $link, "\n";
Output
https://shop.example.com/search?q=red+shoes&page=3
https://shop.example.com/search?q=red+shoes&page=2&sort=price+low
https://shop.example.com/search?q=red+shoes&page=2

Remove a parameter

<?nvs
// Shows how to remove a parameter from the query of an address.
var $link = Core\Uri::parse('/orders?status=open&page=4');
// `null` removes the parameter. This prints /orders?status=open.
echo $link->withQueryParameter('page', null), "\n";
// When the last parameter is removed, the `?` is removed too. This prints /orders.
echo $link->withQueryParameter('page', null)->withQueryParameter('status', null), "\n";
// The other pairs are written again in one standard way. `%20` becomes `+`.
echo Core\Uri::parse('/find?q=blue%20shirt&page=1')->withQueryParameter('page', null), "\n";
Output
/orders?status=open
/orders
/find?q=blue+shirt

Build the links of a page list

<?nvs
// Shows how a list page builds the links to its other pages and keeps the search of the reader.
class Pager
{
public static function links(string $current, int $pages): array<string>
{
var $base = Core\Uri::parse($current);
array<string> $links = [];
int $page = 1;
while ($page <= $pages) {
// The first page gets no `page` parameter, so it has one address only.
if ($page == 1) {
$links[] = $base->withQueryParameter('page', null)->toString();
} else {
$links[] = $base->withQueryParameter('page', $page)->toString();
}
$page = $page + 1;
}
return $links;
}
}
// This prints three links. Each one keeps `q` and `sort`.
foreach (Pager::links('/products?q=lamp&sort=price&page=2', 3) as string $link) {
echo $link, "\n";
}
Output
/products?q=lamp&sort=price
/products?q=lamp&sort=price&page=2
/products?q=lamp&sort=price&page=3