...

View Full Version : $_GET with value of 0?



PeaTearGriffin
05-11-2008, 10:27 PM
Is it possible to use $_GET to pick up a value of 0? It will treat the value of 0 as a value that doesn't exist?

oesxyl
05-11-2008, 10:41 PM
Is it possible to use $_GET to pick up a value of 0? It will treat the value of 0 as a value that doesn't exist?
who? I mean who treat the value as 0 or not exists? From http point of view is a string '0' or "0" without quote, get method take it as it is.
form php point of view it's tricky, avoid this if you can. This kind of value due to the implicit conversion in php can means '', false or 0, is hard to test and induce a lot of problem depending of how you code it.

regards

PeaTearGriffin
05-11-2008, 11:51 PM
who? I mean who treat the value as 0 or not exists?

Oops, sorry was referring to the $_GET. For example, say I have $myid = 0 and then I use the url with the query string id=$myid. If you highlight the link in the browser it will show the query string as ?id=0, but when you click on it $_GET['id'] doesn't grab the value of 0 or it treats it as a null value. What would I need to do to get around this?

oesxyl
05-12-2008, 12:16 AM
Oops, sorry was referring to the $_GET. For example, say I have $myid = 0 and then I use the url with the query string id=$myid. If you highlight the link in the browser it will show the query string as ?id=0, but when you click on it $_GET['id'] doesn't grab the value of 0 or it treats it as a null value. What would I need to do to get around this?
i'm afraid that I don't understand what is the problem.
$_GET don't change the value. For example:



<?php

echo '<a href="'.$_SERVER['PHP_SELF'].'?id=0">click here</a>';
echo '<br/>';
echo $_GET['id'];
?>

if you run this will see that the value displayed under the link is 0. That means that if $myid have the value 0 and is passed correct to the url, as you say, the get method take how it is as 0.
If you post some code or example maybe I can figure out what is the problem.

I hope this help.

regards

PeaTearGriffin
05-12-2008, 01:07 AM
Using your example, this is what I was doing:

<?php

echo '<a href="'.$_SERVER['PHP_SELF'].'?id=0">click here</a>';
echo '<br/>';
if ($_GET['id'])
{
echo "Hello";
}
?>

If the value is 0, it won't work, but if it is 1 it will work. But I tried it with isset and now it works.

PappaJohn
05-12-2008, 01:11 AM
That's because php is evaluating the value 0 as "false", and therefore your conditional statement is false and not executing.

_Aerospace_Eng_
05-12-2008, 01:13 AM
Try this instead

if (isset($_GET['id']))
{
echo "Hello";
}

oesxyl
05-12-2008, 01:16 AM
Using your example, this is what I was doing:

<?php

echo '<a href="'.$_SERVER['PHP_SELF'].'?id=0">click here</a>';
echo '<br/>';
if ($_GET['id'])
{
echo "Hello";
}
?>

If the value is 0, it won't work, but if it is 1 it will work. But I tried it with isset and now it works.
that is implicit conversion. :) 0 is false, :)
using isset make it work no matter if the id is 0 or 100, because isset($_GET['id']) is true, or 1 for any value 0 or non-0.
testing with isset assure you that variable 'id' exists and is passed using get method with any value. Additional you must test the value with == or ===, for example



if(isset($_GET['id']) && $_GET['id'] === 0){
// yes it is 0
}


as I said, this can be tricky.

regards

_Aerospace_Eng_
05-12-2008, 02:03 AM
That will never be true as the $_GET comes in as a string. You need to type cast it or use intval.

<?php
if(isset($_GET['id']) && intval($_GET['id']) === 0){
// yes it is 0
}
?>
however intval returns 0 if the value isn't a number. You should probably use is_numeric().

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
if(intval($_GET['id']) === 0){
echo '$_GET is a number and equals 0';
}
else
{
echo '$_GET is a number but is not equal to 0';
}
}
else
{
echo '$_GET is not a number';
}

oesxyl
05-12-2008, 02:55 AM
That will never be true as the $_GET comes in as a string. You need to type cast it or use intval.

<?php
if(isset($_GET['id']) && intval($_GET['id']) === 0){
// yes it is 0
}
?>
yes you are right, I said in a previous post that is a string but I miss that when I post the snippet. :)



however intval returns 0 if the value isn't a number. You should probably use is_numeric().

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
if(intval($_GET['id']) === 0){
echo '$_GET is a number and equals 0';
}
else
{
echo '$_GET is a number but is not equal to 0';
}
}
else
{
echo '$_GET is not a number';
}
As I said, a lot of complication, better to avoid if you can, :)

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum