← All docs

Math

Mathematical functions: abs, floor, ceil, round, trigonometry, logarithms, and more.

Built-in math functions

FunctionSignatureDescription
abs()abs($val): int|floatAbsolute value (preserves type)
floor()floor($val): floatRound down
ceil()ceil($val): floatRound up
round()round($val [, $precision]): floatRound to nearest
sqrt()sqrt($val): floatSquare root
pow()pow($base, $exp): floatExponentiation
min()min($a, $b, ...): int|floatMinimum (variadic)
max()max($a, $b, ...): int|floatMaximum (variadic)
intdiv()intdiv($a, $b): intInteger division
fmod()fmod($a, $b): floatFloat modulo
fdiv()fdiv($a, $b): floatFloat division (returns INF for /0)
rand()rand([$min, $max]): intRandom integer
mt_rand()mt_rand([$min, $max]): intAlias for rand()
random_int()random_int($min, $max): intCryptographic random
sin()sin($angle): floatSine (radians)
cos()cos($angle): floatCosine (radians)
tan()tan($angle): floatTangent (radians)
asin()asin($val): floatArc sine
acos()acos($val): floatArc cosine
atan()atan($val): floatArc tangent
atan2()atan2($y, $x): floatTwo-argument arc tangent
sinh()sinh($val): floatHyperbolic sine
cosh()cosh($val): floatHyperbolic cosine
tanh()tanh($val): floatHyperbolic tangent
log()log($num [, $base]): floatLogarithm
log2()log2($num): floatBase-2 logarithm
log10()log10($num): floatBase-10 logarithm
exp()exp($val): floate^x
hypot()hypot($x, $y): floatHypotenuse
deg2rad()deg2rad($degrees): floatDegrees to radians
rad2deg()rad2deg($radians): floatRadians to degrees
pi()pi(): floatReturns M_PI

Math constants

ConstantTypeValue
M_PIfloat3.14159265358979…
M_Efloat2.71828182845904…
M_SQRT2float1.41421356237309…
M_PI_2float1.57079632679489…
M_PI_4float0.78539816339744…
M_LOG2Efloat1.44269504088896…
M_LOG10Efloat0.43429448190325…
INFfloatPositive infinity
NANfloatNot a Number
PHP_INT_MAXint9223372036854775807
PHP_INT_MINint-9223372036854775808
PHP_FLOAT_MAXfloat~1.8e308
PHP_FLOAT_MINfloat~2.2e-308
PHP_FLOAT_EPSILONfloat~2.2e-16