phpparse-url

PHP - How to get $_GET Parameter without value


I need to know how to get the $_GET parameter from requested url. If I use for example

$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);

echo $parse_url; will output query=1, but I need only the parameter not parameter AND value to check if parameter is in array.


Solution

  • Based on your example, simply exploding the = might quickly suits your need.

    $url = $_SERVER['REQUEST_URI'];
    $parse_url = parse_url($url, PHP_URL_QUERY);
    $queryparam = explode("=", $parse_url);
    echo $queryparam[0];
    /* OUTPUT query */
    
    if (in_array($queryparam[0], $array_of_params)){ ... }
    

    But you can simply achieve the same thing like this:

    if (@$_GET["query"] != ""){ ... }