You can use this simple function:
function url_get_param($url, $name) { parse_str(parse_url($url, PHP_URL_QUERY), $vars); return isset($vars[$name]) ? $vars[$name] : null; }
See here in action .
It will return the value of the parameter if it exists in the URL, or null if it does not appear at all. You can distinguish a parameter that does not have a value and that does not appear at all with the identical operator (a triple is equal, === ).
This will work with any url you pass, not just $_SERVER['REQUEST_URI'] .
Update:
If you just want to find out if there is any parameter at all in the URL, you can use one of the options above (see Phil's suggestion in the comments).
Or you can use a surprisingly simple test.
if (strpos($url, '=')) {
We donβt even have to worry to check false here, as if an equal sign existed, it would not be the first character.
Update # 2:. Although the strpos method will work for most URLs, it is not bulletproof and should therefore not be used unless you know which URL you are facing. As Steve Onzra correctly points out in the comments, URLs like
http:
Valid and do not contain any parameters.
source share