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.
if ($count % 20 != 0)
if ($count % 20 != 0)
{
// $count is not a multiple of 20
}
If you don't want zero to be excluded:
if ($count % 20 != 0 || $count == 0)
You can do it like so:
if($count % 20 != 0)
Related
I have for loop from 1 to 6000 and I would like to execute a function every 100 times. I have only such an idea:
for($i=0;$i<=6000;$i++)
{
if($i==100 && $i==200 ... $i==6000) do something;
}
How can I solve this problem differently?
From http://php.net/manual/en/language.operators.arithmetic.php
if ($i % 100 == 0) {
// $i can be fully divided by 100
}
The modulo operator (%) tells you if a number divided by another number has a remainder. If the remainder is 0, you know the first number is a multiple of the second (since it divides evenly).
Just check if i is a multiple of 100:
for($i=0;$i<=6000;$i++)
{
if($i % 100 == 0) {
}
}
I agree with the answers this question has received already. However, you might want to omit the case when $i is 0. So you can check it in your for loop if you are starting from 0.
for($i=0; $i<=6000; $i++)
{
if($i != 0 && $i % 100 == 0){
// do something
}
}
I need to know if a changing number divides completely by 4. this is the code that I am currently using:
if ($ResultsCounter %4 != 0){
$htmlResult .= "</div><div class='row'>";
}
Problem here is that if the number is greater than 0 it will always divide by 4. I need this to run if the result is a whole number... i.e. 4, 8, 12, 6...
Change from
if ($ResultsCounter %4 != 0){
to
if ($ResultsCounter %4 == 0){
I am trying to calculate specific values of a counter in my foreach loop.
I have this if statement in my code
if ( $i == 21 || $i == 41 || $i == 61 || $i == 81 || $i == 101 )
which are equal to
($i * 20) + 1
Instead of writing all these values (21,41,61,81...) I want to create a formula for my code but I couldn't figure out what the result should be equal to inside my if statement
Use modulus:
if ($i % 20 == 1) { ...
http://php.net/manual/en/language.operators.arithmetic.php
Look for the remainder after dividing by 20 using the % operator (modulus).
if ($i%20 == 1)
{
// do stuff
}
In php i want to calculate if my count in a foreach loop is a multiple of 3 + 1.
For example if it is 4,7,10,13,16 you get the point.
if( $count % 3 != 0)
This is the closest ive come to finding an answer
What about this?
if ($count % 3 == 1)
This should work:
if ( ($count - 1) % 3 == 0)
Try maybe this:
if ( $count % 3 == 1)
I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.
how can I do that ?
if ($number % 6 != 0) {
$number += 6 - ($number % 6);
}
The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.
If decreasing is acceptable then this is even faster:
$number -= $number % 6;
if ($variable % 6 == 0) {
echo 'This number is divisible by 6.';
}:
Make divisible by 6:
$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
$num += (6-$num%6)%6;
no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.
So you want the next multiple of 6, is that it?
You can divide your number by 6, then ceil it, and multiply it again:
$answer = ceil($foo / 6) * 6;
I see some of the other answers calling the modulo twice.
My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.
Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.
Code: (Demo)
$factor = 6;
for($x = 0; $x < 10; ++$x){ // battery of 10 tests
$number = rand( 0 , 100 );
echo "Number: $number Becomes: ";
if( $remainder = $number % $factor ) { // if not zero
$number += $factor - $remainder; // use cached $remainder instead of calculating again
}
echo "$number\n";
}
Possible Output:
Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66
Use the Mod % (modulus) operator
if ($x % 6 == 0) return 1;
function nearest_multiple_of_6($x) {
if ($x % 6 == 0) return $x;
return (($x / 6) + 1) * 6;
}
Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.
while ($number % 6 != 0) {
$number++;
}
Assuming $foo is an integer:
$answer = (int) (floor(($foo + 5) / 6) * 6)
For micro-optimisation freaks:
if ($num % 6 != 0)
$num += 6 - $num % 6;
More evaluations of %, but less branching/looping. :-P
Why don't you use the Modulus Operator?
Try this:
while ($s % 6 != 0) $s++;
Or is this what you meant?
<?
$s= <some_number>;
$k= $s % 6;
if($k !=0) $s=$s+6-$k;
?>
result = initial number + (6 - initial number % 6)