Friday, October 24, 2025
HomeLanguagesPHP | gmp_perfect_square() Function

PHP | gmp_perfect_square() Function

The gmp_perfect_square() is an inbuilt function in PHP which checks if the given GMP number(GNU Multiple Precision: For large numbers) is a perfect square or not.

Syntax:

gmp_perfect_square($num)

Parameters: The function accepts one GMP number $num. This parameter can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.

Return Value: The function returns true if the given number $num is a perfect square, otherwise it returns false.

Examples:

Input : $num=25 
Output :  true

Input : $num=10
Output :  false

Below programs illustrate the use of gmp_perfect_square() function:

Program 1: The program below demonstrates the working of gmp_perfect_square() function when GMP number is passed as an argument.




<?php
// PHP program to check the if the 
// number is perfect square or not
  
// numeric string arguments 
$num = gmp_init("1001", 2);
// checks if 9 (1001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";  
  
  
$num = gmp_init("11001", 2);
// checks if 25 (11001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";   
  
$num = gmp_init("1100", 2);
// checks if 12 (1100) is a perfect number or not
var_dump(gmp_perfect_square($num));  
?>


Output:

bool(true) 
bool(true)
bool(false)

Program 2: The program below demonstrates the working of gmp_perfect_square() when numeric string is passed as an argument.




<?php
// PHP program to check the if the 
// number is perfect square or not
  
// numeric string arguments 
$num = "9";
// checks if 9 (1001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";  
  
  
$num = "25";
// checks if 25 (11001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";   
  
$num = "12";
// checks if 12 (1100) is a perfect number or not
var_dump(gmp_perfect_square($num));  
?>


Output:

bool(true) 
bool(true)
bool(false)

Reference:
http://php.net/manual/en/function.gmp-perfect-square.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS