UrbanDweller Posted October 5, 2011 Share Posted October 5, 2011 Hey, I have an if statement that requires to check a $_POST to make sure its an integer not a string but cant remember how to do this. Anyone able to assits Thanks Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/ Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 is_numeric() in_int() Should cover you. Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/#findComment-1275933 Share on other sites More sharing options...
Andy-H Posted October 5, 2011 Share Posted October 5, 2011 ctype_digit will (performs an == comparison so will result to boolean(true) for a quoted integer) Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/#findComment-1275934 Share on other sites More sharing options...
UrbanDweller Posted October 5, 2011 Author Share Posted October 5, 2011 Thanks that works, but it seems to be getting stuck at that if statement though even when i enter a number. function checkform(){ $weight = $_POST["weight"]; $null = $_POST["unit_1"]; $null_1 = $_POST["unit_2"]; if(empty($_POST['unit_1']) && empty($_POST['unit_2']) && empty($_POST['submit'])) { form(); } elseif(!is_int($_POST['weight'])) { form(); echo 'enter a number in length'; } else { $unit_2 = $null_1 - 1; $unit_1 = $null - 1; conversion($unit_1, $unit_2, $weight); } } Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/#findComment-1275943 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 As a weight could also have a decimal. I would either use the is_numeric or as Andy-H suggested, ctype_digit (preferred, with no decimal) Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/#findComment-1275947 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 My last post makes no sense If your weight has a decimal, use is_numeric. If it doesnt, try ctype_digit instead of is_int. Thats better. Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/#findComment-1275949 Share on other sites More sharing options...
UrbanDweller Posted October 5, 2011 Author Share Posted October 5, 2011 !ctype_digit works perfect thanks, gonna remember that function. Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/#findComment-1275950 Share on other sites More sharing options...
Pikachu2000 Posted October 5, 2011 Share Posted October 5, 2011 Keep in mind is_numeric will return TRUE for values such as these: 0x3D55AF1 <-- hexadecimal number 42.0345673E105 <-- exponential number Quote Link to comment https://forums.phpfreaks.com/topic/248467-how-to-check-_post-for-a-string/#findComment-1276094 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.