The gmp_cmp() is an inbuilt function in PHP which is used to compare two GMP numbers(GNU Multiple Precision : For large numbers).
Syntax:
gmp_cmp($num1, $num2)
Parameters: This function accepts two GMP numbers $num1 and $num2 as mandatory parameters as shown in the above syntax for comparing. These parameters can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass numeric strings such that it is possible to convert those strings in numbers.
Return Value: The function returns “1” if $num1 > $num2, “0” if $num1 equal to $num2, “-1” if $num1 < $num2.
Examples:
Input : gmp_cmp("1234", "1236")
Output : -1
Input : gmp_cmp("3569", "3569")
Output : 0
Below programs illustrate the gmp_cmp() function in PHP:
Program 1: Program to compare the two GMP numbers when they are passed as numeric strings.
<?php
$num1 = "12356" ;
$num2 = "12356" ;
$res = gmp_cmp( $num1 , $num2 );
echo $res ;
?>
|
Output:
0
Program 2: Program to compare two GMP numbers when they are passed as GMP numbers as arguments.
<?php
$num1 = gmp_init(12355);
$num2 = gmp_init(12356);
$res = gmp_cmp( $num1 , $num2 );
echo $res ;
?>
|
Output:
-1
Reference:
http://php.net/manual/en/function.gmp-cmp.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Apr, 2018
Like Article
Save Article