Cover Image for PHP gmp_add() Function
204 views

PHP gmp_add() Function

As of my last update in September 2021, the gmp_add() function in PHP is used to perform addition between two GMP (GNU Multiple Precision) numbers. GMP is an extension that allows you to work with arbitrary precision numbers, enabling you to perform mathematical operations with very large integers accurately.

Here’s the syntax of the gmp_add() function:

GMP gmp_add ( GMP $num1 , GMP $num2 )

Parameters:

  • $num1: The first GMP number.
  • $num2: The second GMP number.

Return value:

  • The result of the addition as a new GMP number.

Example:

$num1 = gmp_init('12345678901234567890');
$num2 = gmp_init('9876543210987654321');

$result = gmp_add($num1, $num2);

echo gmp_strval($result); // Output: 22222222112222222211

In this example, the gmp_add() function is used to add two large GMP numbers, $num1 and $num2, resulting in a new GMP number with the value 22222222112222222211.

The gmp_add() function is particularly useful when working with numbers that exceed the standard integer size limits in PHP. By using GMP, you can perform arithmetic operations on very large numbers without losing precision.

As with other GMP functions, ensure that the GMP extension is enabled in your PHP configuration to use this function. Additionally, please note that the availability of functions may change in newer PHP versions, so always refer to the PHP manual for the specific version you are using.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS