Computer >> Computer tutorials >  >> Programming >> PHP

PHP – bcpowmod() function


In PHP, bcpowmod() function is used to raise an arbitrary precision base number to another exponent number, reduced by a specified modulus. The bcpowmod() function accepts three arbitrary precision numbers as strings and it returns the base number raised to the exponent modulo number after scaling the result to the specified precision.

Syntax

String bcpowmod($base, $exponent, $modulus, $scale)

Parameters

The bcpowmod() function accepts four different parameters− $base, $exponent, $modulus and $scale.

  • $base− It represents the left operand. It is a string type parameter.

  • $exponent− It represents the right operand number which represents the exponent. It is a string type parameter.

  • $modulus− The $modulus parameter accepts the operand which represents the modulus. It is a string type parameter.

  • $scale− The $scale parameter is an integer type parameter. It states the number of digits which will come after the decimal in the result of (baseexponent%mod). Its default value is 0.

Return Value

The bcpowmod() function returns the result as a string. Or, it returns False if the modulus is 0 or the exponent is negative.

Example 1

<?php
   // input numbers with arbitrary precision
   $base = "5";
   $exponent = "7";
   $mod = "7";

   // calculates the base^exponent % mod
   $result = bcpowmod($base, $exponent, $mod);
   echo "Output without scale: ", $result;
?>

Output

Output without scale: 5

Example 2

<?php
   // input numbers with arbitrary precision
   $base = "5";
   $exponent = "7";
   $mod = "7";

   //Scale value 4
   $scale = 4;

   // calculates the base^exponent % mod
   $result = bcpowmod($base, $exponent, $mod, $scale);
   echo "Output with scale: ", $result;
?>

Output

Output with scale: 5.0000