What is gmp_mod() in PHP?

Overview

The gmp_mod() function in PHP is used for the modulo operation. It accepts two parameters, such as, num1 and num2. The method calculates num1 modulo num2, such as, num1 % num2. The sign of num2 is ignored, and the result is always positive.

Syntax

gmp_mod(GMP|int|string $num1, GMP|int|string $num2): GMP

Parameters

  • num1: This parameter can be a GMP object, integer, or a string.
  • num2: This parameter can be a GMP object, integer, or a string.

Return value

The method returns a GMP object which is the result of num1 mod num2.

Code example

Let’s look at the code below:

<?php
$remainder = gmp_mod(11, 7);
print "11 mod 7 = ".$remainder."\n";
$remainder = gmp_mod("15", 2);
print "15 mod 2 = ".$remainder."\n";
?>

Code explanation

  • Line 2: We obtain 11 mod 7 by invoking gmp_mod() function and pass 11 and 7 as arguments.
  • Line 3: We print the obtained value of remainder/mod value.
  • Line 5: We obtain 15 mod 2 by invoking gmp_mod() function and pass 15 and 2 as arguments. Here, 15 is passed as a string.
  • Line 6: We print obtained the remainder/mod value.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved