FraanXT Posted May 30, 2014 Share Posted May 30, 2014 Hello guys, I have a form where you can select a score between 0 and 10(only integers). I want to check if the value that I recive, is integer. Im using this code(exemple): $integer = intval($_POST["p1"]); if(is_int($integer)){ echo 'INT'; } else { echo 'NO INT'; } My problem is that if it recive letters, the conversion converts it to 0, and it says that it is integer. So with this method I should discard the 0. You guys know a method to avoid that? Thank you guys Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/ Share on other sites More sharing options...
mac_gyver Posted May 30, 2014 Share Posted May 30, 2014 the ctype_digit() function can be used to test if all the characters in a variable are numerical. Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481440 Share on other sites More sharing options...
FraanXT Posted May 30, 2014 Author Share Posted May 30, 2014 the ctype_digit() function can be used to test if all the characters in a variable are numerical. But I want to check if they are integer, if you know how? Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481441 Share on other sites More sharing options...
requinix Posted May 30, 2014 Share Posted May 30, 2014 (edited) ... If all the characters are numerical then it is an integer. Edited May 30, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481442 Share on other sites More sharing options...
FraanXT Posted May 30, 2014 Author Share Posted May 30, 2014 ... If all the characters are numerical then it is an integer. 1 is integer 1,5 isnt integer Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481443 Share on other sites More sharing options...
Jacques1 Posted May 30, 2014 Share Posted May 30, 2014 1,5 isnt integer Who said it is? Did you try ctype_digit()? Or do you just assume it's not what you need? Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481445 Share on other sites More sharing options...
FraanXT Posted May 31, 2014 Author Share Posted May 31, 2014 Who said it is? Did you try ctype_digit()? Or do you just assume it's not what you need? It works, ty bro, and sorry cuz I assumed that it check if its numerical only Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481446 Share on other sites More sharing options...
Jacques1 Posted May 31, 2014 Share Posted May 31, 2014 It does check for digits only. And since a comma isn't a digit, it will not accept “1,5”. Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481447 Share on other sites More sharing options...
FraanXT Posted May 31, 2014 Author Share Posted May 31, 2014 It does check for digits only. And since a comma isn't a digit, it will not accept “1,5”. Yeah nice one, thank you Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481448 Share on other sites More sharing options...
.josh Posted May 31, 2014 Share Posted May 31, 2014 Also wanted to mention that I see in your original code you are using intval. FYI ctype_digit expects a string argument, so if you are converting it to an integer type then you are going to get unexpected results from ctype_digit. So to be clear, do not cast/convert the value as an integer before using ctype_digit. Posted variables should always be a string type, so you don't need to do anything special before using ctype_digit, but if you really want to be explicit, type cast to string: // this should be okay.. if ( ctype_digit( $_POST['p1'] ) ) // ..but if you want to be more explicit if ( ctype_digit( (string) $_POST['p1'] ) ) Quote Link to comment https://forums.phpfreaks.com/topic/288890-check-if-string-is-integer-problem/#findComment-1481450 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.