Skip to content

Core\Uri::query

$uri->query(): ?string

Returns the query of an address, which is the text after the ?. For https://shop.example.com/search?q=shoes&page=2#top the result is q=shoes&page=2.

The query is returned as it was written. Escapes such as %20 and + stay in the text. To read one value from it, use queryParameter(). To read all values into an array, use Core\Uri::parseQuery.

The result is null when the address has no ?. It is "" when the ? has nothing after it. The query ends at the first #, so the fragment is never part of it.

The examples below read the query of a link, show an address with no query and one with an empty query, and pass the query of a request on to another server.

Return value

Returns ?string. The query, without the ?. The result is null when the address has no ?. It is "" when nothing is written after the ?.

Examples

Read the query of a link

<?nvs
// Shows how to read the query of a link, which is the text after the `?`.
var $link = Core\Uri::parse('https://shop.example.com/search?q=red+shoes&page=2#results');
// The query stops before the `#`. This prints q=red+shoes&page=2.
echo $link->query(), "\n";
// The query is returned as it was written, so `%20` stays in the text.
// This prints name=Anna%20Berg&city=Wien.
var $form = Core\Uri::parse('/signup?name=Anna%20Berg&city=Wien');
echo $form->query(), "\n";
// Only the first `?` starts the query. A second `?` is part of the query.
// This prints next=/cart?step=2.
var $login = Core\Uri::parse('/login?next=/cart?step=2');
echo $login->query(), "\n";
Output
q=red+shoes&page=2
name=Anna%20Berg&city=Wien
next=/cart?step=2

No query and an empty query

<?nvs
// Shows the difference between an address with no query and an address with an empty query.
// This address has no `?`, so `query()` returns `null`. This prints "no query".
var $plain = Core\Uri::parse('https://example.com/about');
echo $plain->query() ?? 'no query', "\n";
// This address ends with a `?` and nothing after it. The result is `""`,
// so this prints "empty query".
var $bare = Core\Uri::parse('https://example.com/about?');
?string $query = $bare->query();
if ($query == '') {
echo "empty query\n";
}
// `toString()` gives the address as it was written, so the `?` stays.
// This prints https://example.com/about?.
echo $bare->toString(), "\n";
Output
no query
empty query
https://example.com/about?

Pass a search on to another server

<?nvs
// Shows how to pass the query of a request on to another server, for example a search service.
class Search
{
public static function forward(string $request): string
{
var $incoming = Core\Uri::parse($request);
var $service = Core\Uri::parse('https://search.internal.example.com/v2/find');
// `with` copies the query unchanged. A `null` query removes it.
return $service->with({ query: $incoming->query() })->toString();
}
}
// This prints https://search.internal.example.com/v2/find?q=garden+chair&sort=price.
echo Search::forward('/search?q=garden+chair&sort=price'), "\n";
// This request has no query, so the new address has none either.
// This prints https://search.internal.example.com/v2/find.
echo Search::forward('/search'), "\n";
Output
https://search.internal.example.com/v2/find?q=garden+chair&sort=price
https://search.internal.example.com/v2/find