Demonic Posted July 18, 2008 Share Posted July 18, 2008 <?php $num = 0; $num2 = "0"; $num3 = "0gag"; $num4 = "egaa0"; function is_num($var) { if( preg_match("/^[0-9]+$/", $var) ) { echo 'true<br />'; return; } echo 'false<br />'; return; } is_num($num); is_num($num2); is_num($num3); is_num($num4); ?> I made this function and so far the results are: true true false false Will this always be the case and most likely work in all conditions? Or do I have to go into more in depth checks? Updated with: <?php $num = 0; $num2 = "0"; $num3 = "0gag"; $num4 = "egaa0"; $num5 = true; $num6 = array(); function is_num($var) { if( @preg_match("/^[0-9]+$/", $var) ) { echo 'true<br />'; return; } echo 'false<br />'; return; } is_num($num); is_num($num2); is_num($num3); is_num($num4); is_num($num5); is_num($num6); ?> Results are: true true false false true false Looking good so far, is it safe to say this is an accurate function? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 18, 2008 Share Posted July 18, 2008 It depends on WHAT you are really trying to test. For example this: $num2 = "0"; is really a string. Also, your results above show that this: $num5 = true; returns true for being a number. That doesn't look correct to me. Also your function doesn't take into account numbers that are not integers (e.g. 1.5). There are many built in functions for testing if a variable is a type of number: is_numeric() is_int() is_float() etc. Quote Link to comment Share on other sites More sharing options...
Demonic Posted July 18, 2008 Author Share Posted July 18, 2008 It depends on WHAT you are really trying to test. For example this: $num2 = "0"; is really a string. Also, your results above show that this: $num5 = true; returns true for being a number. That doesn't look correct to me. Also your function doesn't take into account numbers that are not integers (e.g. 1.5). There are many built in functions for testing if a variable is a type of number: is_numeric() is_int() is_float() etc. Regardless $_POST input form posts will end up as strings, and I want to check if its a number, so in my cause will that work? I was looking at 'is_numeric' that would work in this case then right? (Then again I don't want decimals) Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 18, 2008 Share Posted July 18, 2008 Use ctype_digit() Quote Link to comment Share on other sites More sharing options...
Demonic Posted July 18, 2008 Author Share Posted July 18, 2008 Use ctype_digit() Thanks. Quote Link to comment Share on other sites More sharing options...
Demonic Posted July 18, 2008 Author Share Posted July 18, 2008 Had to fix something, since I can't extend or edit the function if it exists: function is_num($var) { return ctype_digit( (string) $var ); } 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.