a way to tell if...is a number?

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
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

a way to tell if...is a number?

Post by hgmmy »

Is there a way to tell if the value of a variable or what have you, is any number? here's a little more info to give you a better idea what I need. I have a form that returns a number that coincides with a user id, and I want a way to make sure that the value is a number as an extra security measure...
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

You could look at the is_int() function. That will tell you if the number is an integer or not.
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

Zoxive wrote:

Code: Select all

$Val = (int) $Val; //Type Casting
http://www.php.net/manual/en/language.t ... ypecasting

is_numeric
is_int

etc..
For this purpose I would not use is_numeric as it will allow for a mjuch wider range than what one would normally associate with a userid. is_int is definitely the best way to go, in this case. :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

is_int() on it's own will always be false. All input comes as a string. If 0 is an invalid id in your database you can use:

Code: Select all

if ((int)$input !== 0) {
    // good
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

ole wrote:is_int() on it's own will always be false. All input comes as a string.
Good point, and one I obviously overlooked...
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Code: Select all

$string = intval($string);
if ($string == 0){
echo "The given variable is not an Integer";
} else {
	echo "The given variable is an Integer";
}
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

ole wrote:is_int() on it's own will always be false. All input comes as a string. If 0 is an invalid id in your database you can use:

Code: Select all

if ((int)$input !== 0) {
    // good
}
that doesn't really matter much....as non existing number also is invalid. simple typecast to (int) is enought IMHO
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

So what would be best? This method:

Code: Select all

if ((int)$input !== 0) {
    // good
}
, this method ctype_digit
or this method:

Code: Select all

if ( $user_ID >= 1 ) {
    //Code to be done...
};
(Note the last method just occurred to me as I read through ya'lls posts)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hgmmy wrote:

Code: Select all

if ( $user_ID >= 1 ) {
    //Code to be done...
};
(Note the last method just occurred to me as I read through ya'lls posts)
Not very effective. To find out why, throw a bunch of stuff at it.
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

I'll just trust you in that it's not very effective, and scratch it off the list.
hgmmy
Forum Commoner
Posts: 25
Joined: Sun Aug 26, 2007 9:16 am

Post by hgmmy »

jmut wrote:simple typecast to (int) is enought IMHO
What do you by typecast, jmut?
Post Reply