Modulus of two variables in PHP

Get an integer remainder with %

The modulus operator % returns the integer remainder after division. It is useful for even/odd checks and repeating patterns.

$number = 17;
if ($number % 2 === 0) {
    echo 'Even';
} else {
    echo 'Odd';
}

This is the recommended starting pattern. The detailed examples below are retained and modernized for additional variations and explanation.

We can get the modulus of two number by using math function modulus (%). This is same as remainder of a math division process of two number. Let us see this by the example script.
fmod(): reminder (modulo) of division
We can see the modulus of 160 and 15 is equal to 10. Same way the reminder or modulus is 0 if two numbers are 150 and 10.
$num1 = 160;
$num2 = 15;
echo "<P>The  value of variables is $num1 &  $num2</P>";
<br>
$result = $num1 % $num2;<br>
echo "<P>The modulus of these numbers  is $result</P>";
Output
The value of variables is 160 & 15

The modulus of these numbers is 10

Finding odd or even number

All even numbers will have modules or reminder as zero when divided by 2.
Here is the code
$number=97;
if($number%2==0){
echo " $number is a even number ";
}else{
echo " $number is an odd number ";
}

Sum of digits of a number

<?php
$t1=1234321; // Sample number  
$sum=0; 
while($t1>0){
	$d=$t1%10; // reminder of division 
	$sum = $sum  + $d;
	$t1=floor($t1/10); // floor value
}
echo $sum;
?>

Reverse the number

<?php
$t1=1234; // Sample data 
$t1_reverse=0; 
while($t1>0){
	$d=$t1%10; // reminder of division 
	$t1_reverse = $t1_reverse * 10 + $d;
	$t1=floor($t1/10); // floor value
}
echo $t1_reverse;
?>
Output
4321

Making alternate rows in different colors

While writing output of a series of records from database table or from any other sources we can make alternate rows of different colors by using modulus function.

Here two style class are defined. Based on modulus value one of them is assigned to as class property of the row of the table.

Here is the code
<html>
<head>
<title>(Type a title for your page here)</title>
<style>
table.t1 tr.r1 td { background-color: #f1f1f1; } 
table.t1 tr.r0 td { background-color: yellow; } 
</style>
</head>

<body>
<?PHp
$i=1;
echo "<table class='t1'>";

for($i=1; $i<=10; $i++){
$m=$i%2;
echo "<tr class='r$m'><td>This is row No : $i</td></tr>";
}

echo "</table>";

?>
</body>
</html>
The output is here.
<table class='t1'><tr class='r1'><td>This is row No : 1</td></tr><tr class='r0'><td>This is row No : 2</td></tr><tr class='r1'><td>This is row No : 3</td></tr><tr class='r0'><td>This is row No : 4</td></tr><tr class='r1'><td>This is row No : 5</td></tr><tr class='r0'><td>This is row No : 6</td></tr><tr class='r1'><td>This is row No : 7</td></tr><tr class='r0'><td>This is row No : 8</td></tr><tr class='r1'><td>This is row No : 9</td></tr><tr class='r0'><td>This is row No : 10</td></tr></table>

Math Functions Absolute value of a number Rounding off value pow() to get exponential value


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer