PHP | gmp_intval() Function

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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"
?> 

chevron_right


Output:

2147

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 vaulue 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"
?>

chevron_right


Output:

7

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



My Personal Notes arrow_drop_up

Contented with little yet wishing for more

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.