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';
}
The % operator works on integers. For floating-point remainders use fmod().
The modulus operator % returns the remainder after integer division. Both operands are converted to integers before the operation.
DivisionByZeroError. For floating-point remainders, use fmod().$num1 = 160;
$num2 = 15;
echo "<p>The values are $num1 and $num2</p>";
$result = $num1 % $num2;
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
$number=97;
if($number%2==0){
echo " $number is a even number ";
}else{
echo " $number is an odd number ";
}
<?php
$t1=1234321; // Sample number
$sum=0;
while($t1>0){
$d=$t1%10; // remainder of division
$sum = $sum + $d;
$t1=intdiv($t1, 10); // remove the last digit
}
echo $sum;
?>
<?php
$t1=1234; // Sample data
$t1_reverse=0;
while($t1>0){
$d=$t1%10; // remainder of division
$t1_reverse = $t1_reverse * 10 + $d;
$t1=intdiv($t1, 10); // remove the last digit
}
echo $t1_reverse;
?>
Output
4321
<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>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.