Posted by: phillipnb | May 25, 2013

PHP and Math

You may be wondering whether it is possible to do arithmetic and mathematical operations in PHP. The answer is “Yes”, we can do arithmetic and mathematical operations in PHP. PHP has a number of built-in mathematical functions. Some of these functions are for rounding, like ceil(),floor(),round(),etc. There are other mathematical functions, grouped as randomization functions like rand() and mt_rand(). In addition to the above there are built-in mathematical functions like abs(), sqrt(), pow(), hypot(), etc. M_PI is a good example for a built-in mathematical constant.

In PHP, you can do basic arithmetic operations using arithmetic operators. For example, if $a = 10 and $b = 5, then addition will be $a + $b, subtraction will be $a – $b, multiplication will be $a * $b and division will be $a / $b. You can read more about arithmetic operators in php from here

The above mentioned arithmetic operators are not useful while working on operations that require high precision. PHP has an answer for this in the form of bcmath library. For doing mathematical operations using numbers, we use the built-in functions from the bcmath library. The php manual says that for arbitrary precision mathematics PHP offers the binary calculator which supports numbers of any size and precision, represented as strings.

Here are a few functions and their explanation from the bcmath library.

bcadd(left operand,right operand,decimal). This function adds two numbers, that is, the left and right operands and the result will be based on the number of decimals specified.

bcsub(left operand,right operand,decimal). This function subtracts the right operand from the left operand.

bcdiv(left operand,right operand,decimal). This function returns the result of the division as a string or NULL if the right operand is 0.

bccomp(left operand,right operand,scale). This function returns 0 if both the operands are equal, 1 if the left operand is greater and -1 if the left operand is smaller.

bcmod(left operand, modulus). This method is used to get the modulus of an arbitrary precision number. This function returns the modulus as a string or NULL if the modulus is zero.

bcmul(left operand, right operand, decimal). This function multiplies the left operand with the right operand and returns the result as a string.

bcpow(left operand, right operand, decimal). Raise the left operand to the power of the right and return the result as a string.

bcpowmod(left operand, right operand, modulus, decimal). Raise an arbitrary precision number to another reduced by a specified modulus.

bscale(scale or decimal). Set the default scale param for all bcmath functions.

bcsqrt(operand, decimal). Get the square root of an arbitrary precision number.

To know more about bcmath functions, please refer the manual

To use bcmath library and its functions, you need to enable it during installation/compilation. Some times, you will be confused whether to use pow() or bcpow() function. The answer to this question is “Generally, you will work with pow() which is slightly faster and also enabled by default. There will be situations in your project where you need to handle very big numbers and then it is wise to use bcpow()”.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: