Page 1 of 1

$_GET values simple question

Posted: Wed Jan 23, 2008 6:53 pm
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!

Re: $_GET values simple question

Posted: Wed Jan 23, 2008 7:12 pm
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.

Re: $_GET values simple question

Posted: Wed Jan 23, 2008 7:25 pm
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().

Re: $_GET values simple question

Posted: Thu Jan 24, 2008 2:01 am
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.