PHP supports and comes with many inbuilt math functions, which always comes handy while performing complex calculation rather than writing your own logic for doing mathematical operations, which can be time-consuming.

We have summarized all important math functions at one place with examples.

So let’s take a look.

 

Math Function name

Description

1.

max

Finds the highest value

2.

min

Finds the lowest value

3.

rand

Generates a random number

4.

srand

Seed the random number generator

5.

getrandmax

Shows the largest possible random value

6.

round

Rounds a float value

7.

pi

Gets the value of PI

8.

pow

Exponential expression

9.

sqrt

Square root

10.

mt_rand

Generates a better random value

11.

mt_srand

Seeds the better random number generator

12.

mt_getrandmax

Shows the largest possible random value

13.

log

Returns the natural logarithm

14.

log1p

Returns log(1+number)

15.

log10

Base 10 logarithm

16.

sin

Sine

17.

cos

Cosine

18.

tan

Tangent

19.

sinh

Hyperbolic Sine

20.

cosh

Hyperbolic Cosine

21.

tanh

Hyperbolic Tangent

22.

asin

Arc Sine

23.

acos

Arc Cosine

24.

atan

Arc Tangent

25.

asinh

Inverse hyperbolic Sine

26.

acosh

Inverse hyperbolic Cosine

27.

atanh

Inverse hyperbolic Tangent

28.

rad2deg

Converts the radian number to the equivalent number in degrees

29.

deg2rad

Converts the number in degrees to the radian equivalent

30.

exp

Calculates the exponent of e

31.

is_nan

Determine whether a value is not a number

32.

ceil

Rounds fractions up

33.

floor

Rounds fractions down

34.

fmod

Returns the floating-point remainder of the division of the arguments

35.

abs

Absolute value

36.

bindec

Converts binary to decimal

37.

decbin

Converts decimal to binary

38.

dechex

Converts decimal to hexadecimal

39.

decoct

Converts decimal to octal

40.

hexdec

Converts hexadecimal to decimal

41.

octdec

Converts octal to decimal

42.

hypot

Returns hypotenuse = sqrt(num1*num1 + num2*num2)

43.

is_finite

Determines whether a value is legal finite number

44.

is_infinite

Determines whether a value is infinite

 

Some examples

<?php

echo "<h3>PHP Math Functions</h3>";

echo "rand() = " . rand() . "<br>"; // any random number

echo "pi() = " . pi() . "<br>"; // 3.1415926535898

echo "round(3.141592653,2) = " . round(3.141592653,2) . "<br>"; // 3.14

echo "pow(3,4) = " . pow(3,4) . "<br>"; // 81

echo "sqrt(25) = " . sqrt(25) . "<br>"; // 5


echo "max(2,6) = " . max(2,6) . "<br>";
// 6

echo "log(10) = " . log(10) . "<br>"; // 2.302585092994

echo "log10(10) = " . log10(10) . "<br>"; // 1


echo "deg2rad(60) = " . deg2rad(60) . "<br>"; // 1.0471975511966

echo "rad2deg(0.78539816339745) = " . rad2deg(0.78539816339745) . "<br>"; // 45

echo "sin(30) = " . sin(deg2rad(30)) . "<br>"; // 0.5

echo "exp(0) = " . exp(0) . "<br>"; // 1

echo "abs(0.10500) = " . abs(0.10500) . "<br>"; // 0.105

echo "hypot(3,4) = " . hypot(3,4) . "<br>"; // 5

echo "decbin(5) = " . decbin(5) . "<br>"; // 101

echo "bindec(101) = " . bindec(101) . "<br>"; // 5

echo "decoct(12) = " . decoct(12) . "<br>"; // 14

echo "octdec(14) = " . octdec(14) . "<br>"; // 12

echo "dechex(15) = " . dechex(15) . "<br>"; // f

?>

Output

PHP Math Functions

rand() = 22515
pi() = 3.1415926535898
round(3.141592653,2) = 3.14
pow(3,4) = 81
sqrt(25) = 5
max(2,6) = 6
log(10) = 2.302585092994
log10(10) = 1
deg2rad(60) = 1.0471975511966
rad2deg(0.78539816339745) = 45
sin(30) = 0.5
exp(0) = 1
abs(0.10500) = 0.105
hypot(3,4) = 5
decbin(5) = 101
bindec(101) = 5
decoct(12) = 14
octdec(14) = 12
dechex(15) = f