Core\Uri::queryParameter
Returns one value from the query of an address, by its name. For /search?q=red+shoes&page=2, queryParameter("q") returns red shoes.
The value is decoded, so + becomes a space and %2F becomes /. It is bytes, because an escape can give any byte. Use as string to get text. When a name appears twice, the last value is kept.
Brackets in a name build arrays, the same way Core\Uri::parseQuery reads them. For size[]=M&size[]=L, queryParameter("size") returns a list with two values.
The result is null when the name is not in the query, and also when the address has no query. Use query() to tell these two apart.
Good to know: each call reads the whole query again. To read many values, call Core\Uri::parseQuery once.
The examples below read one value, read a list and a group of values, and read the page to return to after a login.
Parameters
$namestringThe name of the parameter, without brackets. Fora[b]=c, the name is"a".
Return value
Returns mixed. The decoded value as bytes. Brackets in the name give an array<mixed>. The result is null when the name is not in the query, and also when the address has no query. query() tells these two apart.
Errors
RuntimeErrorA name in the query has escapes that do not decode to valid UTF-8 text.Core\Uri::parseQuerythrows the same error.
Examples
Read one value
<?nvs// Shows how to read one value from the query of a link.
var $link = Core\Uri::parse('https://shop.example.com/search?q=red+shoes&page=2');
// The value is decoded, so `+` becomes a space. The value is `bytes`,// and `as string` turns it into text. This prints "red shoes".echo $link->queryParameter('q') as string, "\n";
// This prints 2.echo $link->queryParameter('page') as string, "\n";
// A name that is not in the query returns `null`. This prints "no sort".if ($link->queryParameter('sort') == null) { echo "no sort\n";}red shoes2no sortRead a list and a group
<?nvs// Shows how to read a list and a group of values from a query.
// A browser writes `[` as `%5B` and `]` as `%5D`. So `size%5B%5D` is `size[]`,// and `filter%5Bcolor%5D` is `filter[color]`.var $link = Core\Uri::parse('/shirts?size%5B%5D=M&size%5B%5D=L&filter%5Bcolor%5D=red');
// `size[]` builds a list. `queryParameter("size")` returns the whole list.// This prints M and L.array<mixed> $sizes = $link->queryParameter('size') as array<mixed>;foreach ($sizes as mixed $size) { echo $size as string, "\n";}
// `filter[color]` builds an array with the key `color`. This prints red.array<mixed> $filter = $link->queryParameter('filter') as array<mixed>;echo $filter['color'] as string, "\n";MLredReturn to a page after login
<?nvs// Shows how a login page reads the page to return to, and accepts only a page on the same site.
class Login{ public static function returnTo(string $request): tainted string { mixed $next = Core\Uri::parse($request)->queryParameter('next'); if ($next == null) { return '/'; } tainted string $path = $next as string; // A path on this site starts with one `/`. `//` or `https:` leads to another site. if (!Core\Str::startsWith($path, '/') || Core\Str::startsWith($path, '//')) { return '/'; } return $path; }}
// This prints /account/orders, /, / and /.echo Login::returnTo('/login?next=%2Faccount%2Forders'), "\n";echo Login::returnTo('/login'), "\n";echo Login::returnTo('/login?next=https%3A%2F%2Fevil.example.net%2F'), "\n";echo Login::returnTo('/login?next=%2F%2Fevil.example.net%2F'), "\n";/account/orders///