Why is there == 0 near some modulo in PHP?

Hello, New to PHP and have seen a few modulo examples in PHP that includeds a == 0. For example:

$myVar1 = 5
5%2 == 0

Why is the == 0? Why do we need it if we just divide && check if there is (and what is) the remainder ?

Correct me if I’m wrong, but == means it equals the ending value which is 0. A single = means that you append that value to the variable which is $myVar1.

Mostly the way I have seen the code snippets provided is in a loop:

<?php

for($i=0; $i<10; $i++) 
{
   if( $i % 3 == 0 )
   {
       echo '<br>';
    }
    echo $i. ', ';
}//

Try running the script and notice when the modulus is equal to zero a line-feed is echoed.

1 Like

For testing if the remainder is equal to that value.

$var % 2 == 0 // test for an even number
$var % 2 == 1 // test for an odd number

if we just divide && check if there is (and what is) the remainder ?

This code is doing rather opposite: it checks whether there is no remainder. (or, more literally, whether remainder is zero)

While when you want to know what the remainder is, you have to write it as

$remainder = $myVar % 2;

Perhaps an easier way to understand what $var %2 == 0 does

Expand the code out to:

$remainder = $var % 2;
if ($remainder == 0)

I like the way PHP Manual explanation that the % is:

Modulus Arithmetic Operator
$a % $b is the Remainder of $a divided by $b.

John, wouldn’t it be more correct to say that it is the pre-remainder value, and not exactly the remainder value,

Why? Because if we follow the algorithm to calculate remainder in general:

Phase A)              y / x = sum.
Phase B)              If sum is a fracture (float), do:
Phase C)              sum / x = pr (pre-remainder value).
Phase D)              Isolate r (remainder value) from pr as by counting only the integer from the pr.

And in a more numerical version:

Phase A)              5 / 2 = 2.5 .
Phase B)              Given that the sum is a fracture (float) do:
Phase C)              2.5/ 2 = 1.25 (pre-remainder value).
Phase D)              Isolate 1 (remainder value) from 1.25 as by counting only the integer from the 1.25 .

So John, I wonder if the guy who wrote the manual actually meant to the pr (pre-remainder value) and not to the actual remainder, because I see no point in dividing the actual remainder as one could understand from the manual…

I think you may be confusing a simple math operation with a more complex calculus function or something.

It’s more like

$dividend = 23;
$divisor = 5;
$quotient = floor ($dividend / $divisor);
$modulus = $dividend - ($divisor * $quotient);
1 Like

The result is an integer (verifed with var_dump($i % 3) ) and considering the result is of a “Black Box Function” I should imagine the algorithm is processed using an arithmetic binary routine. Division is not easy or necessary when only an integer result is required.

A long time ago I learnt Assembler and Machine language arithmetic because it was part of the course. Addition and subtraction was understandable but multiplication and division was horrendous and best forgotten.

1 Like

I am sure I’m not confusing. It’s just a stepped algorithm to extract the remainder even when in mind it might seem a very simple process. For some it’s just more comfortable to use an algorithm, or from any reason they would prefer this way.

1 Like

The modulus (its correct name) doesn’t rely on division for its calculation. The hours minutes and seconds on an analog clock use it with no division required.

Modulo arithmetic simply mans counting to a given number over and over again and never going any higher,

The hours on a clock are %11 + 1.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.