33

I want to find all muliples of a number in PHP.

I'm using something like this

if($count != 20 )

to work out if $count is not equal to 20.

But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc.

Any ideas? I think i need to use the modulus symbol (%), but I don't know.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
dotty
  • 40,405
  • 66
  • 150
  • 195

4 Answers4

73
if ($count % 20 != 0)
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
24
if ($count % 20 != 0)
{
  // $count is not a multiple of 20
}
richsage
  • 26,912
  • 8
  • 58
  • 65
10

If you don't want zero to be excluded:

if ($count % 20 != 0 || $count == 0)
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
marianboda
  • 771
  • 6
  • 14
5

You can do it like so:

if($count % 20 != 0)
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222