Testing the contents of $_GET - the right way

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Testing the contents of $_GET - the right way

Post by maniac9 »

I've been searching for the correct, (and maybe most efficient) way of testing to see if a certain named value exists in the GET variables. Actually, I just need the quickest way to test whether or not a query string was actually supplied.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

if (isset($_GET['some_variable'])) {
   // do something
}
Mark
User avatar
SantaGhost
Forum Commoner
Posts: 41
Joined: Mon Sep 15, 2003 11:54 am

Post by SantaGhost »

for troubleshooting:

Code: Select all

<?php
print_r($_GET);
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I would like to mention this solution also, as a empty string doesn't really need to be correct. Have in mind that the int 0 will also make empty() TRUE.

Code: Select all

if (!empty($_GET['some_variable'])) {
   // do something
}
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you aren't checking for a particular element but just want to check if any GET vars were passed at all:

Code: Select all

<?php
if(isset($_SERVER['QUERY_STRING']))
?>
Post Reply