Skip to content

php_ref_math

PHP provides a comprehensive set of functions for performing mathematical operations, ranging from basic arithmetic and rounding to trigonometric and logarithmic calculations.

These functions operate on PHP’s standard numeric types: int (integers) and float (floating-point numbers, also known as doubles).

The core math functions and constants are built into PHP. No separate installation or extension enabling is required to use them.

FunctionDescription
abs()Returns the absolute (non-negative) value of a number.
ceil()Rounds a number up to the nearest integer.
floor()Rounds a number down to the nearest integer.
round()Rounds a floating-point number to a specified number of decimal places (or to the nearest integer if precision is omitted). Supports different rounding modes (e.g., PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN).
min()Returns the lowest value from a list of arguments or an array.
max()Returns the highest value from a list of arguments or an array.
rand() / mt_rand()Generates a pseudo-random integer. mt_rand() (Mersenne Twister) is generally preferred for better randomness and performance.
srand() / mt_srand()Seeds the random number generator. Usually not needed unless you require a specific repeatable sequence.
getrandmax() / mt_getrandmax()Returns the maximum possible value returned by rand() or mt_rand() respectively.
sqrt()Returns the square root of a number.
pow()Returns a base raised to the power of an exponent (e.g., pow(2, 8) is 2^8).
exp()Calculates the exponent of e (Euler’s number).
log()Returns the natural logarithm (base e) of a number.
log10()Returns the base-10 logarithm of a number.
pi()Returns the approximate value of Pi (π).
is_nan()Checks if a value is ‘Not A Number’ (often resulting from invalid calculations).
is_finite() / is_infinite()Checks if a value is finite or infinite.
intdiv()(PHP 7+) Performs integer division, returning only the integer part of the quotient.
fmod()Returns the floating-point remainder (modulo) of dividing two numbers.
base_convert()Converts a number between arbitrary bases (e.g., binary, octal, decimal, hexadecimal).
bindec(), octdec(), hexdec()Convert binary, octal, or hexadecimal strings to decimal integers.
decbin(), decoct(), dechex()Convert decimal integers to binary, octal, or hexadecimal strings.

PHP includes standard trigonometric functions like sin(), cos(), tan(), asin(), acos(), atan(), atan2(), as well as their hyperbolic counterparts (sinh(), cosh(), tanh(), etc.). It also provides functions for degree/radian conversion (deg2rad(), rad2deg()). Note that these functions typically work with radians.

PHP provides several predefined constants for mathematical values.

ConstantDescription
M_PIValue of Pi (≈ 3.14159)
M_EValue of Euler’s number e (≈ 2.71828)
M_PI_2Pi divided by 2
M_PI_4Pi divided by 4
M_1_PI1 divided by Pi
M_2_PI2 divided by Pi
M_SQRTPISquare root of Pi
M_2_SQRTPI2 divided by the square root of Pi
M_LN2Natural logarithm of 2
M_LN10Natural logarithm of 10
M_LOG2EBase-2 logarithm of e
M_LOG10EBase-10 logarithm of e
M_SQRT2Square root of 2
M_SQRT3Square root of 3
M_SQRT1_2Square root of 1/2
INFRepresents infinity (floating-point value)
NANRepresents ‘Not A Number’ (floating-point value)
PHP_INT_MAXThe largest integer supported in the current PHP build.
PHP_INT_MINThe smallest integer supported in the current PHP build.
PHP_INT_SIZEThe size of an integer in bytes.
PHP_FLOAT_DIGNumber of decimal digits that can be rounded into a float and back without precision loss.
PHP_FLOAT_EPSILONSmallest representable positive number x, so that x + 1.0 != 1.0.
PHP_FLOAT_MINSmallest representable positive floating point number.
PHP_FLOAT_MAXLargest representable floating point number.
PHP_ROUND_HALF_UPRounding mode constant for round(): Rounds halves away from zero.
PHP_ROUND_HALF_DOWNRounding mode constant for round(): Rounds halves towards zero.
PHP_ROUND_HALF_EVENRounding mode constant for round(): Rounds halves towards the nearest even value.
PHP_ROUND_HALF_ODDRounding mode constant for round(): Rounds halves towards the nearest odd value.

For a complete and up-to-date list, always refer to the official PHP documentation.