tags:

views:

64

answers:

5

Hey guys, it's kind of hard to explain but basically I want to detect if any variables have been set through the URL. So with my IF statement all of the following should return true:

and all the following return false:

Any ideas?

A: 

isset($_GET['m'])

or if anything, I believe count($_GET) might work.

Fletcher Moore
Yeh but I don't know what the variables are going to be called, that's my point.
zuk1
A: 

If you mean taking a string and checking if it has a query string, you can use parse_url.

If you mean checking if the current request has a query string, you can just check the length of $_SERVER['QUERY_STRING'].

If you mean to get a count of the number of variables parsed from the query string, you can do count($_GET);

webbiedave
+4  A: 
if( !empty( $_GET ) ) {
   //GET variables have been set
}
Jacob Relkin
+7  A: 

I would test for QUERY_STRING:

if (!empty($_SERVER["QUERY_STRING"]))

should in effect be no different from checking $_GET, though - either way is fine.

Pekka
I like this as it will discover: http://example.com/foo.php?thisisaquery. Notice the lack of equal signs.
jmucchiello
@jmuchhiello true, but even in that case `thisisaquery` should be present in `$_GET` as an empty entry if I'm not mistaken.
Pekka
As always another excellent answer from @Pekka: Subtle difference makes it more correct than other answers
Josh
My answer is actually correct. The difference is too small. Anyway, @Pekka, nice answer.
Jacob Relkin
@Josh thanks, but it's not always that way! I'm glad about the "delete" button sometimes :) @Jacob yup, it'll work either way.
Pekka
@Jacob: Yes, your answer is also correct. I upvoted that one as well.
Josh
@Pekka is `strlen` faster than `empty`?
Jacob Relkin
@Jacob I don't have the benchmarks handy but I strongly doubt it.
Pekka
@Jacob random benchmark says `empty()` is faster: http://maettig.com/code/php/php-performance-benchmarks.php good point. I tend to distrust `empty()` for checks of system variables for some reason (irrational suspicion of whitespaces :) but there is no real reason for it. Changed, cheers.
Pekka
+2  A: 

(count($_GET) > 0)

Mark Baker