PHP | gmp_strval() Function

The gmp_strval() is an inbuilt function in PHP which returns the string value of a GMP number. (GNU Multiple Precision: For large numbers).

Syntax:

string gmp_strval ( GMP $num, int $base )

Parameters: The function accepts two parameters $num and $base as shown above and described below.



  1. $num – The function accepts one GMP number $num and returns its string 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.
  2. $base – This parameter specifies the base of the returned number by the function. The base values for the $base are from 2 to 62 and -2 to -36. This is an optional parameter and the default value is 10.

Return Value: The function returns string value of the given GMP number $num.

Examples:

Input : $num = "110" $base = 2 
Output : 6

Input : $num = "110" 
Output :  110

Below programs illustrate the gmp_strval() function:

Program 1: The program below demonstrates the working of gmp_strval() function when numeric string is passed as an argument and the second parameter is absent.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the gmp_strval() function
   
// when the argument is numeric string and 
// the second parameter is missing
echo gmp_strval("10");  
?>

chevron_right


Output:

10

Program 2: The program below demonstrates the working of gmp_strval() function when numeric string is passed as an argument and the second parameter is present.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the gmp_strval() function
  
  
// when the argument is numeric string and 
// the second parameter is present 
  
echo gmp_strval("10", 2);  
?>

chevron_right


Output:

1010

Program 3: The program below demonstrates the working of gmp_strval() function when GMP number is passed and second parameter is absent.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the gmp_strval() function
   
// when the argument is GMP number and 
// the second parameter is missing 
  
$num = gmp_init("101", 2);
  
//// gmp_strval converts GMP number to string 
// representation in given base(default 10).
echo gmp_strval($num);   
?>

chevron_right


Output:

5

Program 4: The program below demonstrates the working of gmp_strval() function when GMP number is passed as an argument and the second parameter is present.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the gmp_strval() function
  
  
// when the argument is numeric string and 
// the second parameter is present 
$num = gmp_init("1010", 2);  
  
// GMP number in base 8
echo gmp_strval($num, 8);  
?>

chevron_right


Output:

12

Reference:
http://php.net/manual/en/function.gmp-strval.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.