PHP Function Reference

PHP - Math



The PHP has a number of math functions for performing basic numeric operations such as elementary exponential, logarithmic, root and trigonometric functions. These math functions will only handle values within the range of the int and float types on your computer.

Installation

There is no installation needed to use these functions. These functions are part of the PHP core.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

PHP Math Functions

Power & Logarithmic Functions

FunctionsDescription
exp() Returns the exponent of e.
expm1() Returns the exponent of e minus 1, i.e., ex-1.
log() Returns the natural logarithm of a given number.
log10() Returns the base-10 logarithm of a given number.
log1p() Returns the natural logarithm of (1+number), i.e., log(1+number).
sqrt() Returns the square root of the given number.
pow() Returns base raised to the power of exponent.

Rounding & remainder Functions

FunctionsDescription
ceil() Rounds the given number up to the nearest integer.
floor() Rounds the given number down to the nearest integer.
fmod() Returns remainder of the division of arguments.
intdiv() Performs integer division.
fdiv() Divides two numbers, according to IEEE 754.
round() Returns the rounded version of a floating-point number.

Trigonometric Functions

FunctionsDescription
sin() Returns the trigonometric sine of an angle.
cos() Returns the trigonometric cosine of an angle.
tan() Returns the trigonometric tangent of an angle.
asin() Returns the arc sine of a value.
acos() Returns the arc cosine of a value.
atan() Returns the arc tangent of a value.
atan2() Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
hypot() Returns square root of sum of squares of two arguments, i.e., sqrt(x2 +y2).

Hyperbolic Functions

FunctionsDescription
sinh() Returns the hyperbolic sine of a value.
cosh() Returns the hyperbolic cosine of a value.
tanh() Returns the hyperbolic tangent of a value.
asinh() Returns the inverse hyperbolic sine of a value.
acosh() Returns the inverse hyperbolic cosine of a value.
atanh() Returns the inverse hyperbolic tangent of a value.

Angular Conversion Functions

FunctionsDescription
deg2rad() Converts an angle in degrees to an angle in radians.
rad2deg() Converts an angle in radians to an angle in degrees.

Random Number Functions

FunctionsDescription
getrandmax() Returns the largest possible value returned by rand() function.
lcg_value() Returns a pseudo random number in a range between 0 and 1.
mt_getrandmax() Returns the largest possible value returned by mt_srand() function.
mt_rand() Generates a random integer using Mersenne Twister algorithm.
mt_srand() Seeds the Mersenne Twister random number generator.
rand() Generates a random integer.
srand() Seeds the random number generator.

Other Functions

FunctionsDescription
abs() Returns the absolute (positive) value of the given number.
base_convert() Converts a number from one number base to another
bindec() Returns the decimal equivalent of the binary number represented by the binary string argument.
decbin() Returns a string which is the binary representation of a decimal number.
dechex() Returns a string which is the hexadecimal representation of a decimal number.
decoct() Returns a string which is the octal representation of a decimal number.
hexdec() Returns the decimal equivalent of the hexadecimal number represented by the hexadecimal string argument.
is_finite() Checks whether a value is finite or not
is_infinite() Checks whether a value is infinite or not
is_nan() Checks whether a value is 'not-a-number'
max() Returns the maximum value in an array, or the maximum value among specified values.
min() Returns the minimum value in an array, or the minimum value among specified values.
octdec() Returns the decimal equivalent of the octal number represented by the octal string argument.
pi() Returns the value of PI.

PHP Math Predefined Constants

PHP has predefined Math Constants. The constants below are always available as part of the PHP core.

ConstantsValueDescription
M_PI3.14159265358979323846Value of pi
M_E2.7182818284590452354Value of e
M_LOG2E1.4426950408889634074Value of log_2 e
M_LOG10E0.43429448190325182765Value of log_10 e
M_LN20.69314718055994530942Value of log_e 2
M_LN102.30258509299404568402Value of log_e 10
M_PI_21.57079632679489661923Value of pi/2
M_PI_40.78539816339744830962Value of pi/4
M_1_PI0.31830988618379067154Value of 1/pi
M_2_PI0.63661977236758134308Value of 2/pi
M_SQRTPI1.77245385090551602729Value of sqrt(pi)
M_2_SQRTPI1.12837916709551257390Value of 2/sqrt(pi)
M_SQRT21.41421356237309504880Value of sqrt(2)
M_SQRT31.73205080756887729352Value of sqrt(3)
M_SQRT1_20.70710678118654752440Value of 1/sqrt(2)
M_LNPI1.14472988584940017414Value of log_e(pi)
M_EULER0.57721566490153286061Value of Euler constant
PHP_ROUND_HALF_UP1Rounds number away from zero when half way.
PHP_ROUND_HALF_DOWN2Rounds number towards zero when half way.
PHP_ROUND_HALF_EVEN3Rounds number towards the nearest even value when half way.
PHP_ROUND_HALF_ODD4Rounds number towards the nearest odd value when half way.
NANNAN (as a float)Not A Number
INFINF (as a float)The infinite

Example:

In the example below, math constants are used to show its value.

<?php
echo "M_PI = ".M_PI."\n";
echo "M_PI_2 = ".M_PI_2."\n";
echo "M_PI_4 = ".M_PI_4."\n";
echo "M_E = ".M_E."\n";
echo "M_LN10 = ".M_LN10."\n";
echo "M_SQRT2 = ".M_SQRT2."\n";
echo "M_SQRT3 = ".M_SQRT3."\n";
echo "M_SQRT1_2 = ".M_SQRT1_2."\n";
?>

The output of the above code will be:

M_PI = 3.1415926535898
M_PI_2 = 1.5707963267949
M_PI_4 = 0.78539816339745
M_E = 2.718281828459
M_LN10 = 2.302585092994
M_SQRT2 = 1.4142135623731
M_SQRT3 = 1.7320508075689
M_SQRT1_2 = 0.70710678118655