| ||||||
|
How to find out if a value is dividable by a specified number in PHP
Here is a useful bit of code for testing whether or not a number is dividable by a specified value. In our example below we will go through a "for" loop, checking every number to see if it can be divided by 5. If yes we will print it, if not we shall skip it and go on to the next. So how do you find out if a number is dividable by another? Simple, all that is needed is to see if after a division a remainder exists. To do this we simply place a percentage sign between the two numbers: if(!($i%5)){ ... } If there is no remainder, then the number is clearly dividable. For a closer look at how remainders work in PHP, see the following tutorial. <?php for($i = 0; $i != 100; $i++) { if(!($i%5)) { echo $i."<br>"; } } ?>
|