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

?>

Link to comment
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;
    }
}

?>

 

Dont worry I added that myself :) Thanks again.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.