Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 is_int() won't tell you if a numeric string represents an integer; is_int('123'); returns FALSE. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1268839 Share on other sites More sharing options...
AbraCadaver 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. If the field type is INT then what else could be returned except for a string containing an INT? Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1268843 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 is_int() won't tell you if a numeric string represents an integer; is_int('123'); returns FALSE. Oh, I misread. That's funny. You'd think they would have one. I suppose: $val = intval($val); echo is_int($val); Would work. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1268844 Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 This seems to have morphed into a conversation on more of a theoretical/philosophical plane. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1268845 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 This seems to have morphed into a conversation on more of a theoretical/philosophical plane. Indeed. It may have to be closed for its own benefit since now I've become part of the problem I was talking about earlier. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1268846 Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 I suppose: $val = intval($val); echo is_int($val); Would work. Actually no. If $val isn't a valid integer then intval() will return 0. It always returns an integer so is_int(intval($variable)) is always true. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1268885 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 I suppose: $val = intval($val); echo is_int($val); Would work. Actually no. If $val isn't a valid integer then intval() will return 0. It always returns an integer so is_int(intval($variable)) is always true. That's pretty annoying. I would say check if the value was 0 but 0 itself is a possible integer. Seems like you would have to check if the variable was 0 then echo intval. Seems overcomplicated. I'll stick to not using 0 as an integer. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1269005 Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 Seems overcomplicated. I'll stick to not using 0 as an integer. I'd stick to assuming that values from an INT column are, in fact, integers. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1269010 Share on other sites More sharing options...
xyph Posted September 13, 2011 Share Posted September 13, 2011 I really like Pikachu's solution (modified slightly) <?php $values = array( 15.1, -20, '4524', 15, 'zomg', '-434', '12.5', '--10' ); foreach( $values as $v ) { var_dump($v); echo ( isInt($v) ? '<font color="green">is ' : '<font color="red">is not ' ).' an integer or integer string</font><br>'; } function isInt( $n ) { $n = (string) $n; return ctype_digit($n[0]=='-'?substr($n,1):$n); } ?> Or one line return ctype_digit( substr($n,0,1)=='-'?substr($n,1):(string)$n ); Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1269011 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 I'd stick to assuming that values from an INT column are, in fact, integers. Definitely. Link to comment https://forums.phpfreaks.com/topic/246972-how-can-check-variable-value-content-integer-or-not-by-is_int/page/2/#findComment-1269012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.