a way to tell if...is a number?
Moderator: General Moderators
a way to tell if...is a number?
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...
Code: Select all
$Val = (int) $Val; //Type Castingis_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.Zoxive wrote:http://www.php.net/manual/en/language.t ... ypecastingCode: Select all
$Val = (int) $Val; //Type Casting
is_numeric
is_int
etc..
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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
}- crystal ship
- Forum Commoner
- Posts: 36
- Joined: Wed Aug 29, 2007 5:45 am
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";
}that doesn't really matter much....as non existing number also is invalid. simple typecast to (int) is enought IMHOole 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 }
So what would be best? This method:
, this method ctype_digit
or this method:
(Note the last method just occurred to me as I read through ya'lls posts)
Code: Select all
if ((int)$input !== 0) {
// good
}or this method:
Code: Select all
if ( $user_ID >= 1 ) {
//Code to be done...
};- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Not very effective. To find out why, throw a bunch of stuff at it.hgmmy wrote:(Note the last method just occurred to me as I read through ya'lls posts)Code: Select all
if ( $user_ID >= 1 ) { //Code to be done... };