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!
$_GET values simple question
Moderator: General Moderators
-
masterkrang
- Forum Newbie
- Posts: 10
- Joined: Wed Jan 23, 2008 6:45 pm
Re: $_GET values simple question
Isset is working like it was made to.
What your looking for is the empty function.
Take alook at the php manual, it gives plenty of examples of empty, and isset.
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';
}
?>- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: $_GET values simple question
As Zoxive points out, you need to do something like:
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().
Code: Select all
if (isset($_GET['variable'] && ($_GET['variable'] != '')) {(#10850)
-
masterkrang
- Forum Newbie
- Posts: 10
- Joined: Wed Jan 23, 2008 6:45 pm
Re: $_GET values simple question
Thanks a lot guys. Gonna try those out and refer to the manual about empty function, sounds like it's what I need.