Jump to content

Check if value is increment of 5


dc_jt

Recommended Posts

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

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;
    }
}

?>

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.

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!';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.