asmith Posted December 24, 2007 Share Posted December 24, 2007 hey guys when i check this : if (is_int($_POST[number])) it always return false , seems like $_POST always send a string . is_int will return true for 2, NOT for "2" . how can i check an input that it is numeric ? ( is_numeric allow float too but i just whole numbers.) and i don't mean by preg_match . thanks in advance Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/ Share on other sites More sharing options...
emehrkay Posted December 24, 2007 Share Posted December 24, 2007 is_numeric will take "2" and cast it as a number and then test to see if it is a number Return Values Returns TRUE if var is a number or a numeric string, FALSE otherwise. maybe you need to do if(is_numeric($_POST['number']) && is_int($_POST['number'])){} Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422525 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 $number = $_POST['number']; $int = (int) $number; if($int == $number) { //integer } //OR.... if(ctype_digit($_POST['number'])) { //integer } Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422526 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 is_numeric will take "2" and cast it as a number and then test to see if it is a number Return Values Returns TRUE if var is a number or a numeric string, FALSE otherwise. maybe you need to do if(is_numeric($_POST['number']) && is_int($_POST['number'])){} That will always return false.... is_numeric does the casting internally and the variable isn't passed by reference. That means, the second time, it will still be a string.... Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422528 Share on other sites More sharing options...
asmith Posted December 24, 2007 Author Share Posted December 24, 2007 ctype_digit ! that was a function i had never heard of , works perfect . thanks guys for taking time why this function is not so popular anyway ? Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422532 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 I don't know.... I hadn't ever heard of it either until the other day when someone posted about it in a thread similar to this thread. Converting the types and comparing (forcing int) is actually a little bit faster, but it's obviously more code, and it's like .00000000000000001 seconds faster. Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422534 Share on other sites More sharing options...
asmith Posted December 24, 2007 Author Share Posted December 24, 2007 thanks for the information Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422546 Share on other sites More sharing options...
emehrkay Posted December 24, 2007 Share Posted December 24, 2007 is_numeric will take "2" and cast it as a number and then test to see if it is a number Return Values Returns TRUE if var is a number or a numeric string, FALSE otherwise. maybe you need to do if(is_numeric($_POST['number']) && is_int($_POST['number'])){} That will always return false.... is_numeric does the casting internally and the variable isn't passed by reference. That means, the second time, it will still be a string.... i think you are confused. you want to change what is held in the $_POST var by casting it as an integer $int = (int) $number; not me. I want to check to see if what is in there is a number, and if it is an integer I doubted my logic since you brought it into question and created a function to test what I was saying. Run it <?php function numberAndIntegerCheck($x){ if(is_numeric($x) && is_int($x)){ echo '<br />$x is a number and an integer!'; }else{ echo '<br />fail!'; } } numberAndIntegerCheck(4); numberAndIntegerCheck("4"); ?> Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422547 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 C:\Users\Corbin\Desktop>php post.php <br />$x is a number and an integer!<br />fail! C:\Users\Corbin\Desktop> "That will always return false...." Unless you do like you said and cast the $_POST vars, that will indeed always return false, since $_POST variables are strings. If you meant doing that, like you said in your second post, then I guess I missed that part. Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422551 Share on other sites More sharing options...
emehrkay Posted December 24, 2007 Share Posted December 24, 2007 $number = $_POST['number']; $int = (int) $number; if($int == $number) { //integer } additionally, if you want to correct code, this doesnt make any sense. you are casting $number as an intger and using a == to check against dataypes - wrong! when checking datatypes you use the triple equal sign === and you dont even need to compare the two vars, just trying to cast a new datatype is check enough $x = "asfdasf4"; if((integer) $x){ echo 'that is a number'; } Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422557 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 Ahhh I didn't know you could do an if on a cast. Also, $number = $_POST['number']; $int = (int) $number; if($int === $number) { //this would always be false } $number would be a string, and $int would be an integer, thus, they need to be compared with == so that $int will be converted back to a string for comparison. Edit: (Actually, it might convert $number to a integer, in which case, this code would break since if $number failed at conversion the first time and was set to zero, it would fail the second time, and the two 0s would match.) Link to comment https://forums.phpfreaks.com/topic/83064-solved-is_int-vs-is_numeric-noobish-question/#findComment-422564 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.