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 Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/ 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. Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/#findComment-891975 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 } ?> Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/#findComment-891977 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 Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/#findComment-891980 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! Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/#findComment-891982 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; } } ?> Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/#findComment-891984 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. Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/#findComment-891987 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!'; } Link to comment https://forums.phpfreaks.com/topic/169060-check-if-value-is-increment-of-5/#findComment-892012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.