Open In App

PHP | gmp_intval() Function

Last Updated : 20 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The gmp_intval() is an inbuilt function in PHP which converts a GMP number to an integer. Here GMP refers to GNU Multiple Precision which is for large numbers.
Syntax: 
 

int gmp_intval ( $num )

Parameters: The function accepts a single parameter $num which is a GMP number and returns its integer value. 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 integer value of the given GMP number $num
Examples: 
 

Input : $num = "2147483647"
Output :  2147483647

Input : $num = "12"
Output :  12

Note: If a numeric string is passed as an integer, it returns the same integer (except above PHP integer limit). But if a GMP number is passed, it returns the integer value of a GMP number. 
Below programs illustrate the use of gmp_intval() function:
Program 1: The program below demonstrates the working of gmp_intval() function when numeric string is passed as an argument. 
 

php




<?php
// PHP program to demonstrate the gmp_intval()
// function when argument is passed
 
$x = gmp_intval("2147") . "\n";
 
// prints integer value of a gmp number
 
// it returns the same numeric string in integer form
echo gmp_strval($x) . "\n";
?>


Output: 
 

2147

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

php




<?php
// PHP program to demonstrate the gmp_intval() function
// when GMP number is passed as an argument
  
// arguments as GMP numbers
$num = gmp_init("1111", 2); // num initialisation = 12
  
// integer value of GMP number 12 is 12
$x = gmp_intval($num);
  
// prints the integer value of a gmp number
// gmp_strval converts GMP number to string
// representation in given base(default 10).
echo gmp_strval($x) . "\n";
?>


Output: 
 

7

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads