Forum und email

GMP Functions


These functions allow you to work with arbitrary-length integers using the GNU MP library.

These functions have been added in PHP 4.0.4.

Note: Most GMP functions accept GMP number arguments, defined as resource below. However, most of these functions will also accept numeric and string arguments, given that it is possible to convert the latter to a number. Also, if there is a faster function that can operate on integer arguments, it would be used instead of the slower function when the supplied arguments are integers. This is done transparently, so the bottom line is that you can use integers in every function that expects GMP number. See also the gmp_init() function.


If you want to explicitly specify a large integer, specify it as a string. If you don't do that, PHP will interpret the integer-literal first, possibly resulting in loss of precision, even before GMP comes into play.

Note: This extension is available on Windows platforms since PHP 5.1.0.

요구 조건

You can download the GMP library from » This site also has the GMP manual available.

You will need GMP version 2 or better to use these functions. Some functions may require more recent version of the GMP library.


In order to have these functions available, you must compile PHP with GMP support by using the --with-gmp option.

실행시 설정

이 확장은 php.ini 설정이 존재하지 않습니다.


Most GPM functions operate on or return GMP number resources.

예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

GMP_ROUND_ZERO (integer)
GMP_VERSION (string)
The GMP library version


Example#1 Factorial function using GMP

function fact($x
$return 1;
    for (
$i=2$i $x$i++) {
$return gmp_mul($return$i);

gmp_strval(fact(1000)) . "\n";

This will calculate factorial of 1000 (pretty big number) very fast.


More mathematical functions can be found in the sections BCMath Arbitrary Precision Mathematics Functions and Mathematical Functions.

Table of Contents