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 arrayvaluewrites the brackets.$valuemixedThe new value. It can be a scalar or an array. An array is written with brackets, the same wayCore\Uri::buildQuerywrites it.nullremoves 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 whenvalueis not a scalar, an array ornull, 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";https://shop.example.com/search?q=red+shoes&page=3https://shop.example.com/search?q=red+shoes&page=2&sort=price+lowhttps://shop.example.com/search?q=red+shoes&page=2Remove 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";/orders?status=open/orders/find?q=blue+shirtBuild 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";}/products?q=lamp&sort=price/products?q=lamp&sort=price&page=2/products?q=lamp&sort=price&page=3