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?
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:
This works because $_GET is an array, and an empty array will be evaluated
false, while a populated array istrue.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$_GETarray. I also usually assign to an array inside theif– 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.