Thursday, May 14, 2026
HomeLanguagesPHP | gmp_sub() Function

PHP | gmp_sub() Function

The gmp_sub() is an in-built function in PHP which returns the subtraction of the two GMP numbers.(GNU Multiple Precision: For large numbers)

Syntax:

gmp_sub($num1, $num2)

Parameters: This function accepts two GMP numbers $num1 and $num2 as mandatory parameters shown in the above syntax. These parameters 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 the subtraction of two numbers $num1 and $num2.

Examples:

Input : $num1=5 , $num2=10
Output : -5 

Input : $num1=7 , $num2=1
Output : 6 

Below programs illustrate the gmp_sub() function:

Program 1: The program below demonstrates the working of gmp_sub() function when GMP number are passed as arguments.




<?php
// PHP program to subtract two 
// two numbers 
  
// GMP number as arguments 
$num1 = gmp_init("101", 2);
$num2 = gmp_init("1010", 2); 
  
// 5-10 = -5 
$sub = gmp_sub($num1, $num2); 
  
// gmp_strval converts GMP number to string 
// representation in given base(default 10).
echo gmp_strval($sub, 2);
?>


Output:

-101

Program 2: The program below demonstrates the working of gmp_sub() when numeric string are passed as arguments.




<?php
// PHP program to subtract two 
// two numbers 
  
// numeric strings number as arguments 
$num1 = "7";
$num2 = "1";
  
// 7-1 = 6
$sub = gmp_sub($num1, $num2); 
  
echo $sub;
?>


Output:

6

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

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS