doni49 Posted November 8, 2006 Share Posted November 8, 2006 Recently, I needed to figure out if a number is a multiple of another number.i.e. is $test a multiple of 5, is it a multiple of 7 etc. I thought I'd share the results.[code]//if $test is a multiple of 7 then the following will return true.is_int($test/7);[/code]The reason for this is that if you divide a number by another number and it returns an integer, then it was evenly divisible by that number. If dividing doesn't result in an integer then it's a decimal value which means that it wasn't evenly divisible by that number. Quote Link to comment https://forums.phpfreaks.com/topic/26523-how-to-tell-if-a-variable-is-a-multiple-of-2-3-4-etc/ Share on other sites More sharing options...
fert Posted November 8, 2006 Share Posted November 8, 2006 you could have just used % Quote Link to comment https://forums.phpfreaks.com/topic/26523-how-to-tell-if-a-variable-is-a-multiple-of-2-3-4-etc/#findComment-121357 Share on other sites More sharing options...
doni49 Posted November 8, 2006 Author Share Posted November 8, 2006 Okay from what I can see $test % 7 would return TRUE if NOT a multiple of 7 and FALSE if it IS a multiple of 7.Well there's two simple ways thanks. I hunted and hunted then it dawned on me that i could just divide it and see if the result was an integer. Quote Link to comment https://forums.phpfreaks.com/topic/26523-how-to-tell-if-a-variable-is-a-multiple-of-2-3-4-etc/#findComment-121361 Share on other sites More sharing options...
redbullmarky Posted November 8, 2006 Share Posted November 8, 2006 that's what modulus is. it's the remainder after division has been performed - so it actually returns a number, not true/false.[code]<?phpecho 15 % 7; // gives 1echo 14 % 7; // gives 0echo 8 % 7; // gives 1?>[/code]so in your case, you're actually checking to see if the result is zerocheers Quote Link to comment https://forums.phpfreaks.com/topic/26523-how-to-tell-if-a-variable-is-a-multiple-of-2-3-4-etc/#findComment-121441 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.