Open In App

PHP | gmp_sign() Function

Last Updated : 14 Apr, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The gmp_sign() is an in-built function in PHP which checks the sign of a given GMP number (GNU Multiple Precision: For large numbers).

Syntax:

gmp_sign($num)

Parameters: This function accepts one GMP number $num as mandatory parameter shown in the above syntax. 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 checks the sign of the given number $num and returns three values depending on the number as described below:

  • Returns 1 – $num is positive
  • Returns -1 – $num is negative
  • Returns 0 – $num is zero

Examples:

Input : $num=9
Output : 1 

Input : $num=-8
Output : -1 

Input : $num=0
Output : 0 

Below programs illustrate the gmp_sign() function:

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




<?php
// PHP program to check the sign 
// of a number 
  
// GMP arguments 
// negative 
$num1 = gmp_init("-101", 2);
  
// positive
$num2 = gmp_init("1010", 2); 
  
// zero
$num3 = gmp_init("0", 2); 
  
  
// prints -1 as negative
echo gmp_sign($num1)."\n"
  
// prints +1 as negative
echo gmp_sign($num2)."\n";  
  
// prints 0 as 0
echo gmp_sign($num3)."\n"
  
?>


Output:

-1
1
0

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




<?php
// PHP program to check the sign 
// of a number 
  
// numeric arguments 
  
// negative 
$num1 = -9;
  
// positive
$num2 = 8;
  
// zero
$num3 = 0;
  
  
// prints -1 as negative
echo gmp_sign($num1)."\n"
  
// prints +1 as negative
echo gmp_sign($num2)."\n";  
  
// prints 0 as 0
echo gmp_sign($num3)."\n"
  
?>


Output:

-1
1
0

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads