Page 1 of 1

hard question to ask, but probably simple to answer

Posted: Tue Jun 02, 2009 3:43 am
by zaeon
Ok I've been having a hard time trying to search for a solution to this on the internet, I'm hoping somebody with experience could lend a few moments to help with this...

I'm passing variables between pages with forms using GET method, and to make it simpler let's say for instance I have a variable called NUMBER, and I want to run code if a NUMBER is being passed.. Sounds simple, and I thought this would work...

Code: Select all

 
$mynum = $_GET['NUMBER'];
 
if ($mynum) {
...main code
}
 
and it does work.. except when NUMBER = 0.. which is my problem because 0 is passed just as often as any other number..

this is my solution in the meantime.. i've set up a dummy variable NUMTRUE on the posted form and then I'm doing this...

Code: Select all

 
if ($_GET['NUMTRUE']) {
...main code
}
 
that way when NUMBER=0, it will be treated like any other number, but that seems inefficient to me.. is there a better way to do this?

Re: hard question to ask, but probably simple to answer

Posted: Tue Jun 02, 2009 3:47 am
by Mark Baker

Code: Select all

 
if ($mynum) {
...main code
}
 
You're treating $mynum as a boolean; and 0 boolean is false

Code: Select all

if (is_numeric($mynum)) {

Re: hard question to ask, but probably simple to answer

Posted: Tue Jun 02, 2009 3:55 am
by zaeon
wow so fast!! thank you.. works perfectly

Re: hard question to ask, but probably simple to answer

Posted: Tue Jun 02, 2009 9:12 am
by mikemike
Be careful here, is_numeric might accept things you don't want. Personally I use ctype-digit() because I don't want to except numbers like '1e4'.