hard question to ask, but probably simple to answer

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
zaeon
Forum Newbie
Posts: 2
Joined: Tue Jun 02, 2009 3:19 am

hard question to ask, but probably simple to answer

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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)) {
zaeon
Forum Newbie
Posts: 2
Joined: Tue Jun 02, 2009 3:19 am

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

Post by zaeon »

wow so fast!! thank you.. works perfectly
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

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

Post 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'.
Post Reply