dc_jt Posted August 6, 2009 Share Posted August 6, 2009 Hi I have a form where a user enters a value and I need to check if the value is an increment of 5 i.e 5,10,15,20,25 etc. I am thinking of dividing the value they enter by 5 and if it is a round number then it is correct if not then it is incorrect but does anyone know how to check this? Is there an is_round() function or anything similar? Thanks Quote Link to comment Share on other sites More sharing options...
mattal999 Posted August 6, 2009 Share Posted August 6, 2009 I think I'll have a go at making you a function: <?php is_divisible_by($number, $divby) { $number = $number / $divby; if(is_int($number)) { return true; } else { return false; } } ?> Untested code. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 6, 2009 Share Posted August 6, 2009 <?php if (!($foo%5)) { // this IS a multiple of 5 as %foo%5 will evaluate to 0 ie false } else { // %foo%5 >0 so a remainer exists } ?> Quote Link to comment Share on other sites More sharing options...
dc_jt Posted August 6, 2009 Author Share Posted August 6, 2009 Thanks for the quick replies. ToonMariner's almost works but it accepts values such as 10.50 which it shouldnt. Is there a way around this? Thanks Quote Link to comment Share on other sites More sharing options...
dc_jt Posted August 6, 2009 Author Share Posted August 6, 2009 Infact it looks as though mattal999 works fine, thanks! Quote Link to comment Share on other sites More sharing options...
mattal999 Posted August 6, 2009 Share Posted August 6, 2009 Infact it looks as though mattal999 works fine, thanks! No problem. Just noticed that I forgot the function declaration. <?php function is_divisible_by($number, $divby) { $number = $number / $divby; if(is_int($number)) { return true; } else { return false; } } ?> Quote Link to comment Share on other sites More sharing options...
dc_jt Posted August 6, 2009 Author Share Posted August 6, 2009 Infact it looks as though mattal999 works fine, thanks! No problem. Just noticed that I forgot the function declaration. <?php function is_divisible_by($number, $divby) { $number = $number / $divby; if(is_int($number)) { return true; } else { return false; } } ?> Dont worry I added that myself Thanks again. Quote Link to comment Share on other sites More sharing options...
Adam Posted August 6, 2009 Share Posted August 6, 2009 10.50 would work because PHP converts all numbers to integers before using with modulus (%), effectively leaving 10. You could just add to the if condition though and save yourself the need for a function (unless you plan on re-using this code several times?) or to simplify the function: if (!$x % 5 || strstr($x, '.')) { echo 'not a multiple!'; } else { echo 'is a multiple!'; } 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.