$_GET values simple question

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
masterkrang
Forum Newbie
Posts: 10
Joined: Wed Jan 23, 2008 6:45 pm

$_GET values simple question

Post by masterkrang »

Hi, I'm new here, thanks for having me.

I am having an issue where I'm trying to bring in variables using $_GET. Let's say I have a URL like this: somepage.php?blah=something&variable=

I am using isset($_GET['variable']) and it is returning true. Since that variable isn't set, I'm assuming it's just telling me that a variable called 'variable' exists? My goal is to figure out whether or not a variable is there, and if it is there, I want to then check whether or not that variable has a value associated with it.

Another thing I found odd was that the sizeOf a variable that hadn't been set in the URL string would return 1. For example, take a URL like this: somepage.php?var=

then: echo sizeOf($_GET['var']); //outputs 1

Any help? Thanks!
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: $_GET values simple question

Post by Zoxive »

Isset is working like it was made to.

What your looking for is the empty function.

Code: Select all

<?php
$var = '';
 
// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}
 
// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>
Take alook at the php manual, it gives plenty of examples of empty, and isset.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: $_GET values simple question

Post by Christopher »

As Zoxive points out, you need to do something like:

Code: Select all

if (isset($_GET['variable'] && ($_GET['variable'] != '')) {
Also, sizeof() (which is an alias to count()) gives the number scalars in a variable and is commonly used for arrays. It will return 1 for non-array vars. You want to use strlen().
(#10850)
masterkrang
Forum Newbie
Posts: 10
Joined: Wed Jan 23, 2008 6:45 pm

Re: $_GET values simple question

Post by masterkrang »

Thanks a lot guys. Gonna try those out and refer to the manual about empty function, sounds like it's what I need.
Post Reply