The modulo operator in PHP is represented by the percentage symbol %. It calculates the remainder of one number divided by another. The basic syntax for the modulo operator is-
Syntax:
$result = $num1 % $num2;
Here, $num1 is the dividend, and $num2 is the divisor. The result will be the remainder after dividing $num1 by $num2.
Example 1: This example describes the Basic Modulo Operation.
PHP
<?php $dividend = 10;
$divisor = 3;
$result = $dividend % $divisor ;
echo "Remainder is: $result" ;
?> |
Output
Remainder is: 1
Example 2: This example checks for Even or Odd Numbers.
PHP
<?php $number = 7;
if ( $number % 2 == 0) {
echo "$number is an even number." ;
} else {
echo "$number is an odd number." ;
} ?> |
Output
7 is an odd number.