PHP Math Functions (original) (raw)

Last Updated : 17 Apr, 2025

PHP is a scripting language that comes with many built-in math functions and constants. These tools help you do basic math, solve more complex problems, and work with different math concepts. This guide gives an overview of PHP's math functions and constants, along with examples, practical uses, and helpful tips.

**Note: The PHP math functions are built into PHP, so you don't need to install anything to use them.

PHP Math Functions

PHP offers numerous mathematical functions that allow you to perform operations like rounding, trigonometry, logarithms, and more. Here are the most commonly used functions:

**Math Function **Description
abs() Returns the absolute value of a number.
acos() Returns the arc cosine (inverse of cosine) of a number in radians.
acosh() Returns the inverse hyperbolic cosine of a number.
asin() Returns the arc sine (inverse of sine) of a number in radians.
asinh() Returns the inverse hyperbolic sine of a number.
atan() Returns the arc tangent (inverse of tangent) of a number in radians.
atan2() Returns the arc tangent of the two variables in radians.
atanh() Returns the inverse hyperbolic tangent of a number.
base_convert() Converts a number between different number bases.
bindec() Converts a binary number to decimal.
ceil() Rounds a number up to the nearest integer.
cos() Returns the cosine of an angle in radians.
cosh() Returns the hyperbolic cosine of an angle in radians.
decbin() Converts a decimal number to a binary number.
dechex() Converts a decimal number to a hexadecimal number.
decoct() Converts a decimal number to an octal number.
deg2rad() Converts an angle from degrees to radians.
exp() Returns e raised to the power of a given number.
expm1() Returns e raised to the power of a number minus 1.
floor() Rounds a number down to the nearest integer.
fmod() Returns the remainder of the division of two numbers.
getrandmax() Returns the largest possible random value returned by rand().
hexdec() Converts a hexadecimal number to a decimal number.
hypot() Returns the square root of the sum of squares of its arguments.
intdiv() Performs integer division (returns the quotient of two numbers).
is_finite() Checks whether a number is finite.
is_infinite() Checks whether a number is infinite.
is_nan() Checks whether a value is NaN (Not a Number).
lcg_value() Returns a pseudo-random number in the range between 0 and 1.
log() Returns the natural logarithm (base e) of a number.
log10() Returns the base-10 logarithm of a number.
log1p() Returns the logarithm of 1 + number.
max() Returns the highest value from a given set of numbers.
min() Returns the lowest value from a given set of numbers.
mt_getrandmax() Returns the largest possible value returned by mt_rand().
mt_rand() Generates a random number using the Mersenne Twister algorithm.
mt_srand() Seeds the Mersenne Twister random number generator.
octdec() Converts an octal number to a decimal number.
pi() Returns the value of Pi (π).
pow() Returns the result of raising a number to the power of another number.
rad2deg() Converts an angle from radians to degrees.
rand() Generates a random integer.
round() Rounds a floating-point number to the nearest integer.
sin() Returns the sine of an angle in radians.
sinh() Returns the hyperbolic sine of an angle in radians.
sqrt() Returns the square root of a number.
srand() Seeds the random number generator.
tan() Returns the tangent of an angle in radians.
tanh() Returns the hyperbolic tangent of an angle in radians.

PHP Predefined Math Constants

PHP provides several predefined constants for mathematical operations. These constants can be used directly in your calculations without the need to define them.

**Constant **Value **Description
INF INF Represents infinity.
M_E 2.7182818284590 The mathematical constant e (Euler's number).
M_EULER 0.5772156649015 The Euler's constant, often used in various mathematical formulas.
M_LNPI 1.1447298858494 The natural logarithm of Pi (log_e(pi)).
M_LN2 0.6931471805599 The natural logarithm of 2 (log_e(2)).
M_LN10 2.3025850929940 The natural logarithm of 10 (log_e(10)).
M_LOG2E 1.4426950408890 The logarithm of e with base 2 (log_2(e)).
M_LOG10E 0.4342944819033 The logarithm of e with base 10 (log_10(e)).
M_PI 3.1415926535898 The value of Pi (π), the ratio of a circle's circumference to its diameter.
M_PI_2 1.5707963267949 Pi divided by 2, used in trigonometry for 90 degrees (π/2).
M_PI_4 0.7853981633974 Pi divided by 4, used in trigonometry for 45 degrees (π/4).
M_1_PI 0.3183098861830 The reciprocal of Pi (1/π).
M_2_PI 0.6366197723670 The reciprocal of 2 * Pi (2/π).
M_SQRTPI 1.7724538509055 The square root of Pi (√π).
M_2_SQRTPI 1.1283791670955 Two times the reciprocal of the square root of Pi (2/√π).
M_SQRT1_2 0.7071067811865 The square root of 1/2 (√(1/2)), also equal to 1/√2.
M_SQRT2 1.4142135623731 The square root of 2 (√2).
M_SQRT3 1.7320508075689 The square root of 3 (√3).
NAN NAN Represents a value that is "Not A Number", usually due to undefined mathematical operations.
PHP_ROUND_HALF_UP 1 Rounds a number up when it is halfway between two integers.
PHP_ROUND_HALF_DOWN 2 Rounds a number down when it is halfway between two integers.
PHP_ROUND_HALF_EVEN 3 Rounds a number to the nearest even integer when it is halfway between two integers.
PHP_ROUND_HALF_ODD 4 Rounds a number to the nearest odd integer when it is halfway between two integers.

Best Practices for Using PHP Math Functions

Conclusion

PHP offers a wide range of math functions that make it easier for developers to perform tasks like basic math, trigonometry, and logarithms. These built-in tools help simplify coding, ensuring your calculations are accurate and efficient.