rockyraw
rockyraw

Reputation: 1145

How to check for ANY parameter existence in the URL?

When I access any page.php version, that contains ANY parameter, such as:

page.php?foo=1 or page.php?bar=3

I want to set a value for a $var.

I only know how to check if a specific parameter exists:

if (isset($_GET['foo']))
$var = "yes";

But how can I check if any parameter exists?

Upvotes: 1

Views: 86

Answers (4)

elixenide
elixenide

Reputation: 44851

Examine the contents of the $_GET array as a whole:

if (!empty($_GET)) {
    // do something
}

Upvotes: 2

fillobotto
fillobotto

Reputation: 3785

$_GET is an array containing all available GET parameters.

So just cycle them with a foreach:

foreach ($_GET as $key=>$value) {
    echo "$key = " . urldecode($value) . "<br />\n";
}

To be clear: take foo=1 example. $key vill results in foo, while $value will results in 1.

Upvotes: 0

Dimas Pante
Dimas Pante

Reputation: 2521

Check with:

if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { 
    ...
}

It brings all the data existent after the ? symbol

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 79024

Check if $_GET is not empty. This is a faster call than count:

if(!empty($_GET)) {
    // parameter exists
}

Upvotes: 1

Related Questions