Alan Edwardes

Using PHP isset To Detect An Empty GET Request

Old Content Warning

This post is very old (it was published 5 years ago), so I can't vouch for its content or accuracy. It may be here for posterity. Please take its content with a pinch of salt.

Here's something I learnt a while back, about how to detect if there is a ?foo after the page URL, even if that GET request is empty (without the =bar) bit. Try it on my "Random Post" button, you will find it adds a ?r after the post URL you are sent to, so the page knows to add an ID that makes the button highlighted.

So, to detect it:

if (isset($_GET['foo'])) echo('Yeah, foo is set');

This would work on something like http://www.example.com/index.php?foo, and avoids an unnecessary ?foo=yes or something to indicate that foo is true. Neat and tidy :-)

2 Comments

Sarthak's Gravatar
1

well the if condition based on isset($_GET['var']) works fine with echo etc... but the problem arises when u try to use the get variable inside the if condition in something like.... if(isset($_GET['var'])) { $var2=$_GET['var']; } else { $var2=$var3; } searched a lot on google and found many people having this problem ..

Add a Comment

06th of June 2008 at 2:09 PM

4 years, 11 months ago

I was 16 years old when I wrote this

100 words

2 comments so far

feed for comments on this post

rand: Asus Eee PC 1101HA <3 ...

next: Windows Vista - The ...

prev: Firefox Auto Download Security ...

share:FacebookTwitterRedditdiggStumbleUpondeliciousHacker NewsLinkedIn

add a comment

© 2006 – 2013 Alan Edwardes / Source on GitHub
Top