2

Possible Duplicate:
How to check if $_GET is empty?

I'm trying to check for the condition that there is any $_GET data in my urls ?var=foo&biz=bang, and the if statement I'm currently using to do this is isset($_GET) //Do something, but it's return truey even though there isn't even any ? or & anywhere in my URL. I'm assuming that the $_GET variable is always set, so how do I check for this?

Duplicate of "How to check if $_GET is empty?"

Community
  • 1
  • 1
Kevin Beal
  • 10,500
  • 12
  • 66
  • 92

3 Answers3

7

Since $_GET is always set in php (when run in a webserver), what you really want to know is "is anything in there?"

You want to use:

if ($_GET) {
}

This works because $_GET is an array, and an empty array will be evaluated false, while a populated array is true.


UPDATE

I no longer use the above method, since it has really negative effects on testability and CLI-run commands (phpunit, artisan).

I now always check isset() on the $_GET array. I also usually assign to an array inside the if - php evaluates these expressions to the value of the assigned variable.

If your framework has a "safe array access" function, you should always use it. If you framework has a "safe GET function" that's even better. If you aren't using a framework, or your framework does not include these helper functions, you should write them for yourself.

// example with "safe array access"
if (isset($_GET) && $value = safe_array($_GET, 'key')) {
    // do something with $value
}

// example with "safe GET function"
if ($value = safe_input_get('key')) {
    // do something with $value
}
willoller
  • 7,106
  • 1
  • 35
  • 63
  • That didn't work. Are you sure empty arrays return false? – Kevin Beal Oct 20 '12 at 01:00
  • Whoops! My bad. I left a `!` in there. Your code definitely works :P – Kevin Beal Oct 20 '12 at 01:13
  • Upvoted and commented so hopefully the top (wrong) answer isn't accepted – James L. Oct 20 '12 at 01:39
  • @Rambo brings up a good point: $_GET will not be set if you are running from the CLI or other non-web method. This is a real problem for testability, so you should wrap your calls to $_GET in a function to handle exceptions (missing keys) and let you inject values when urls are unavailable. – willoller Oct 20 '12 at 05:41
4
if(!empty($_GET))
{
    // do something
}

That should let you know if the get array is populated

Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • 1
    $_GET is always set in a web enviornment, which makes !empty slightly redundant. – goat Oct 20 '12 at 01:30
  • Rambo is correct and this answer is NOT correct.. $_GET is often set without any $_GET string being passed in the uri – James L. Oct 20 '12 at 01:37
0

using empty() could possibly be problematic since it will return true for 0, 0.00, false, and others (http://php.net/manual/en/function.empty.php)

Possible workarounds:

<?php
function is_blank($value) {
    return empty($value) && !is_numeric($value);
}
if (!is_blank($_GET)){
   ... do your thing ...
}

?>

or check for a specific post value isset($_POST['value'])

Horen
  • 11,184
  • 11
  • 71
  • 113
  • $_GET will never have a value or `0` or `0.00` unless userland code has mucked with it. Some of the entries in the array may have those values, but that is irrelevant to whether or not $_GET converts to boolean true or false. An array coerces to false only when it has no array keys. – goat Oct 20 '12 at 01:32
  • couldn't $_GET be false though? – Horen Oct 20 '12 at 01:35
  • 1
    No. It could be completely undefined(which is null, which in turn then coerces to false) if you're running php in a non web environment, for example, via the command line. Or, if the php config has been set to not populate that superglobal(extremely uncommon in a web environment), it again will be undefined. I'm of the opinion that extenuating circumstances like these should throw errors/notices, because they truely are error conditions which the code has not anticipated and isnt programmed to handle. – goat Oct 20 '12 at 01:41