man12_patil3 Posted September 12, 2011 Share Posted September 12, 2011 $showCountSql = "select cad_count from counteraccountdtl WHERE cad_userid =".$_SESSION['UID']." LIMIT 1"; $showCountresult = mysql_query($showCountSql); $showCountrow = mysql_fetch_array($showCountresult); $newCount = $showCountrow[cad_count]; if(is_int($newCount)) echo "Value is Integer"; else echo "Value not Integer"; i m fetching value by Mysql "cad_count integer(5)" and feild then i cheak this value is interger or not it show the "Value not Integer". What is wrong in it ??? Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/ Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 is the field type INT..? or VARCHAR Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268373 Share on other sites More sharing options...
man12_patil3 Posted September 12, 2011 Author Share Posted September 12, 2011 field type is int(5). Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268374 Share on other sites More sharing options...
Psycho Posted September 12, 2011 Share Posted September 12, 2011 EDIT: field type is int(5). Then why do you need to validate it if the database is going to enforce it as an int? Original Text: What type of field is "cad_count" in the database? Is it a varchar or float type? If the value is always supposed to be an int then you should be using that type of field. However, if the field can be other than an int, then you could validate an int using ctype_digit() to verify that it is a positive integer. If you need to allow negative integers I can give you a different solution. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268376 Share on other sites More sharing options...
PFMaBiSmAd Posted September 12, 2011 Share Posted September 12, 2011 Database functions return strings, by definition, regardless of the field type. From the php documentation for the function you are trying to use - is_int — Find whether the type of a variable is integer Description bool is_int ( mixed $var ) Finds whether the type of the given variable is integer. Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268379 Share on other sites More sharing options...
AyKay47 Posted September 12, 2011 Share Posted September 12, 2011 yeah I don't really understand the logic for checking if it is an int coming right out of your db.. as to why the condition is failing.. I'm not sure really.. perhaps it is returning a numeric string.. hard to say.. EDIT: PFM confirmed my thoughts... well noted Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268381 Share on other sites More sharing options...
AbraCadaver Posted September 12, 2011 Share Posted September 12, 2011 No matter what type the DB field is the PHP array value will be a string. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268382 Share on other sites More sharing options...
Psycho Posted September 12, 2011 Share Posted September 12, 2011 To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). The problem with is_numeric() is that it will return true for floats. Most times when I need something to be an int I typically need it to be an int with a value of 1 or greater. Such as when it need to be the primary id for a record. In that case I typically use something such as $value = (int) $value; //Force value to be an integer if($value<1) { //Validation failed } But, it all depends on what are valid values for the variable you are using as to how you should validate it. But, as stated previously, if the field is an int type in the database - it will be an int. Unless of course you allow a null value in that field. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268390 Share on other sites More sharing options...
xyph Posted September 12, 2011 Share Posted September 12, 2011 In verifying an idea to check for integers, I have made my head hurt. Can anyone explain this behavior? <?php $a = 'aager12345a'; $b = $a; $c = (int) $b; var_dump($a); echo '<br>'; var_dump($c); echo '<br>'; if( $a == $c ) { echo 'match'; } else { echo 'no match'; } ?> Outputs string(11) "aager12345a" int(0) match PHP 5.3.0 Apache 2.2.11 Windows 7 - Local PHP 5.2.17 Apache ?? Redhat Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268422 Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 Since $c was cast as an integer, I suspect php is type-juggling $a to an integer as well. That would cause its value in the comparison to be 0, allowing the comparison to evaluate to TRUE. A strict === comparison returns FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268431 Share on other sites More sharing options...
AbraCadaver Posted September 12, 2011 Share Posted September 12, 2011 Since $c was cast as an integer, I suspect php is type-juggling $a to an integer as well. That would cause its value in the comparison to be 0, allowing the comparison to evaluate to TRUE. A strict === comparison returns FALSE. Yes, http://php.net/manual/en/types.comparisons.php. Except I believe that $a is juggled to a float. But still 0 == 0.0 and (int)0 == (float)0. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268444 Share on other sites More sharing options...
xyph Posted September 12, 2011 Share Posted September 12, 2011 Ah, makes sense. Odd I've never run into an issue like this. Guess I've been more careful than I thought when it comes to comparisons. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268462 Share on other sites More sharing options...
requinix Posted September 12, 2011 Share Posted September 12, 2011 ctype_digit, unless the value can be negative. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268511 Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2011 Share Posted September 12, 2011 That's easy to compensate for, too. if( ctype_digit(trim(str_replace('-', '', $num))) ) { Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268531 Share on other sites More sharing options...
xyph Posted September 13, 2011 Share Posted September 13, 2011 That's easy to compensate for, too. if( ctype_digit(trim(str_replace('-', '', $num))) ) { If you change that to ctype_digit(trim(ltrim($num,'-'))) it will only match '-1554' and not '15-54' Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268729 Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 Ah, yes! I didn't test that possibility. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268760 Share on other sites More sharing options...
Psycho Posted September 13, 2011 Share Posted September 13, 2011 I don't know if this is more efficient or not, but it is easier to "see" the logic in my opinion: if(is_numeric($val) && intval($val)==$val) EDIT: Just found that the above returns true for something such as '12.0'. But, you could just wrap the above in a function and convert to an int before returning the value. This really shouldn't be that hard. If there is a strtotime() function there should be a function to validate if a string represents an integer or not. EDIT #2: OK, this seems to work for any values. if((string) (int) $val === (string) $val) Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268766 Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 I, personally, would rather use a regular expression than try a bunch of type casting. if (preg_match('/^[+-]?\d+\.?$/', $val)) // integer Or a quick scan through for a period. if (is_numeric($val) && strpos($val, ".") === false) Or rounding. if (round($val) == $val) But still: why are you trying to validate an integer when you already know it's an integer? Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268805 Share on other sites More sharing options...
AyKay47 Posted September 13, 2011 Share Posted September 13, 2011 I, personally, would rather use a regular expression than try a bunch of type casting. if (preg_match('/^[+-]?\d+\.?$/', $val)) // integer Or a quick scan through for a period. if (is_numeric($val) && strpos($val, ".") === false) Or rounding. if (round($val) == $val) But still: why are you trying to validate an integer when you already know it's an integer? not sure how this thread got so hot.. there is no logic to begin with Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268810 Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 I don't even know what the question was anymore. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268811 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 I really don't understand why this thread has caused so much confusion. Reg expressions to check if a variable is an integer... really? Why? Retrieving items from the database will give you an array of strings. So simply convert the strings stored in the integer field to integers either by type casting or using intval(). It's that simple. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268816 Share on other sites More sharing options...
AyKay47 Posted September 13, 2011 Share Posted September 13, 2011 I don't even know what the question was anymore. something about Star Wars i think.. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268821 Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 I really don't understand why this thread has caused so much confusion. Reg expressions to check if a variable is an integer... really? Why? Yeah, I know. But apparently an INT field type in the database isn't enough. And it's true that PHP doesn't have a function to check if a numeric string is strictly an integer. Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268828 Share on other sites More sharing options...
AyKay47 Posted September 13, 2011 Share Posted September 13, 2011 prevent anything other then an integer getting inserted into the table... nothing other than an integer will be output.. simple as that Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268835 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 Yeah, I know. But apparently an INT field type in the database isn't enough. And it's true that PHP doesn't have a function to check if a numeric string is strictly an integer. ? http://www.php.net/manual/en/function.is-int.php Quote Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/#findComment-1268836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.