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
}