What is gmp_perfect_power in PHP?

The gmp_perfect_power, a function in PHP, checks if the argument passed is a perfect powerA perfect power is an integer that can be expressed as a power b where both a and b are integers greater than one or not.

Syntax

gmp_perfect_power(GMP|int|string $num): bool

Parameters

The function takes in one parameter: num.


num can be a numeric string, GMPGNU Multiple Precision Arithmetic Library object, or of the int type.


Return value

The function returns a boolean value. The function returns true if num is a perfect power. Otherwise, it returns false.

Code

Let’s run the following code to check whether a number is a perfect power or not using gmp_perfect_power() function.

<?php
$val1 = gmp_perfect_power('169');
if ($val1 == true) {
echo "val1 is a Perfect Power";
} else {
echo "val1 is not a Perfect Power";
}
?>

Line 2: Assigns the result of checking if the number 169 is a perfect power using the gmp_perfect_power() function to the variable $val1.

Lines 3–7: Starts an if statement to check if the value of $val1 equals true. If yes, then it prints "val1 is a Perfect Power". Otherwise, "val1 is not a Perfect Power".

Free Resources