Search code examples
php

PHP to check if a URL contains a query string


This is an easy one. There seem to be plenty of solutions to determine if a URL contains a specific key or value, but strangely I can't find a solution for determining if URL does or does not have a query at all.

Using PHP, I simply want to check to see if the current URL has a query string. For example: http://abc.com/xyz/?key=value VS. http://abc.com/xyz/.


Solution

  • For any URL as a string:

    if (parse_url($url, PHP_URL_QUERY))
    

    http://php.net/parse_url

    If it's for the URL of the current request, simply:

    if ($_GET)