RE: [PHP] An integer?

From: Date: Mon, 21 Aug 2000 22:27:38 +0000
Subject: RE: [PHP] An integer?
Groups: php.general 
Request: Send a blank email to php-general+get-12814@lists.php.net to get a copy of this message
so what is the final solution on this? Sincerely berber Visit http://www.weberdev.com Today!!! To see where PHP might take you tomorrow. > -----Original Message----- > From: Lars Torben Wilson [mailto:torben@pinc.com] > Sent: Monday, August 21, 2000 9:58 PM > To: Joao Prado Maia > Cc: php-general@lists.php.net > Subject: RE: [PHP] An integer? > > > Joao Prado Maia writes: > > At 08:18 PM 8/21/2000 +0100, you wrote: > > > > > > This may be a silly question, but I can't spot and easy > way to do it, I > > > > want to check if the value inside a vriable is an > integer. If I do > > > > is_int($variable) I always get false, because this > variable qwas read in > > > > from a form so its a string, if I make the variable an > integer using > > > > settype() I just end up with 0 if it was invalid, the > number otherwise. I > > > > need something that will actually tell me if there the > conversion was > > > > successfull. Am I missing something? > > > > > >if(!((int)$variable)) { > > > //Not an int or 0 > > >} else { > > > // An int != 0 > > >} > > > > He can also use gettype() > > > > if (gettype($variable) == "integer") { > > // do stuff > > } else { > > // do other stuff > > } > > > > http://www.php.net/manual/function.gettype.php > > > > Regards, > > Joao > > No, neither of these examples will do what he wanted--he's trying to > find out whether a given variable (a string, since it came from a > form) represents an integer. The gettype() test above will always > evaluate to false, since form data is always passed as a string (or an > array of strings). > > The 'if(!((int)$variable))' just tests whether the result of > converting > the incoming string to an int is a true value or not. Among other > things, this means that it doesn't consider '0' an integer, but does > consider '45otherstuff' and integer (since the conversion of > '45otherstuff' to int results in '45'. > > -- > +----------------------------------------------------------------+ > |Torben Wilson <torben@php.net> Netmill iTech| > |http://www.coastnet.com/~torben > http://www.netmill.fi| > |Ph: 1 250 383-9735 torben@netmill.fi| > +----------------------------------------------------------------+ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: > php-list-admin@lists.php.net >

« previous php.general (#12814) next »