Friday, 3 June 2016

PHP- CHECK IF A NUMBER IS MULTIPLE OF ANOTHER NUMBER

This is one of those posts that is purely a reminder for myself. It’s something so simple, but for some reason or another, I tend to forget.
Suppose I have a loop, and I want to call a certain action on every 5th loop. For example, on the 5th, 10th, 15th..etc count, I want to call an action.
1
2
3
4
5
6
7
8
9
<?
for($i = 1; $i < 21; $i++)
{
 if ($i % 5 == 0)
 {
  echo "Hello World";
 }
}
?>
In the example above, “Hello World” will echo on the 5th, 10th, 15th and 20th count.

0 comments:

Post a Comment