PHP | gmp_cmp() Function

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to compare two
// GMP numbers passed as arguments 
   
// strings as GMP numbers 
$num1 = "12356";
$num2 = "12356";
  
// compares the two numbers and
// gives the result "0" as both are equal 
$res = gmp_cmp($num1, $num2);
  
echo $res;
  
?>

chevron_right


Output:

0

Program 2: Program to compare two GMP numbers when they are passed as GMP numbers as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to compare two
// GMP numbers passed as arguments 
  
// creating GMP numbers using gmp_init()
$num1 = gmp_init(12355);
$num2 = gmp_init(12356);
  
// compares these two numbers and
// gives the result "-1" as $num1 < $num2
$res = gmp_cmp($num1, $num2);
  
echo $res;
?>

chevron_right


Output:

-1

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.