Horje
How to use Modulo Operator in PHP ?

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.






Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
PHP Program to Check if a String Contains Uppercase, Lowercase, Special Characters and Numeric Values PHP Program to Check if a String Contains Uppercase, Lowercase, Special Characters and Numeric Values
How to Access Child Method From the Parent in VueJS ? How to Access Child Method From the Parent in VueJS ?
How to Set Max and Min Value for Y Axis in Chart.js ? How to Set Max and Min Value for Y Axis in Chart.js ?
Asia Cup Country Wise Winners List (1984-2023) Asia Cup Country Wise Winners List (1984-2023)
How to Become a Software Developer in 2024 | Roadmap with Detailed Steps How to Become a Software Developer in 2024 | Roadmap with Detailed Steps

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13