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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 ); Quote Link to comment 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. Quote Link to comment 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.