×
Author:
Website:
Page title:
URL:
Published:
Last revised:
Accessed:

PHP Math Function Reference

PHP has a number of built-in functions that are specifically designed to facilitate mathematical calculations, some of which we have already encountered in this section. The following table lists the PHP math functions available at the time of writing and gives a brief description of each (the descriptions shown are based on information taken from the PHP Group’s online manual for PHP 8.4).

Index



PHP Math Functions
FunctionDescription
abs() Absolute value - accepts a numeric value as its argument and returns the absolute value of the argument as an integer or float (depending on the numeric type of the argument). For example:

$num = -1234;
var_dump(abs($num));
// int(1234)

$num = -456.789;
var_dump(abs($num));
// float(456.789)

acos() Arc cosine - accepts a floating-point value in the range -1.0 to 1.0 as its argument and returns the angle (in radians) for which that value is the cosine. Passing a value outside the range -1.0 to 1.0 to acos() results in a return value of NAN. For example:

var_dump(acos(-1));
// float(3.141592653589793)

var_dump(acos(1.5));
// float(NAN)

acosh() Inverse hyperbolic cosine - accepts a floating-point value as its argument and returns the inverse hyperbolic cosine of that value, i.e. the value whose hyperbolic cosine has that value. For example:

var_dump(acosh(10.0));
// float(2.993222846126381)

asin() Arc sine - accepts a floating-point value in the range -1.0 to 1.0 as its argument and returns the angle (in radians) for which that value is the sine. Passing a value outside the range -1.0 to 1.0 to asin() results in a return value of NAN. For example:

var_dump(asin(-1));
// float(-1.5707963267948966)

var_dump(asin(1.5));
// float(NAN)

asinh() Inverse hyperbolic sine - accepts a floating-point value as its argument and returns the inverse hyperbolic sine of that value, i.e. the value whose hyperbolic sine has that value. For example:

var_dump(asinh(10.0));
// float(2.99822295029797)

atan() Arc tangent - accepts a floating-point value as its argument and returns the angle (in radians) for which that value is the tangent. For example:

var_dump(atan(10));
// float(1.4711276743037347)

var_dump(atan(-10));
// float(-1.4711276743037347)

atan2() Arc tangent of two variables - accepts two floating-point values as its arguments. The arguments represent the x and y coordinates of a point in the Cartesian plane. The return value is the angle (in radians) between the positive x-axis and the point (the sign of both arguments is used to determine the quadrant of the result). For example:

var_dump(atan2(10, 10));
// float(0.7853981633974483)

var_dump(atan2(-10, -10));
// float(-2.356194490192345)

atanh() Inverse hyperbolic tangent - accepts a floating-point value in the range -1.0 to 1.0 as its argument and returns the inverse hyperbolic tangent of that value, i.e. the value whose hyperbolic tangent has that value. Passing a value outside the range -1.0 to 1.0 to atanh() results in a return value of NAN. For example:

var_dump(atanh(1.1));
// float(NAN)

var_dump(atanh(1));
// float(INF)

var_dump(atanh(0.5));
// float(0.5493061443340549)

var_dump(atanh(0));
// float(0)

var_dump(atanh(-0.5));
// float(-0.5493061443340549)

var_dump(atanh(-1));
// float(-INF)

var_dump(atanh(-1.1));
// float(NAN)

base_convert() Convert a number between arbitrary bases - accepts three arguments. The first argument is the string representation of the number to be converted, which must be a positive integer value. Quotes may be omitted, except for hexadecimal numbers (or numbers of other arbitrary number bases) that contain non-numeric characters. The second argument is a positive integer value specifying the base the number is in, and the third argument is a positive integer value specifying the base to which the number is to be converted. Returns a string representation of the converted number. For example:

var_dump(base_convert("25", 10, 2));
// string(5) "11001"

var_dump(base_convert(11111111, 2, 10));
// string(3) "255"

var_dump(base_convert("ff00", 16, 10));
// string(5) "65280"

var_dump(base_convert(77, 8, 10));
// string(2) "63"

bindec() Binary to decimal conversion - accepts the string representation of the binary number to be converted, which must be a positive integer value, as its argument. Quotes may be omitted unless the binary number includes leading zeros. Returns the value as a base 10 integer. For example:

var_dump(bindec(1111));
// int(15)

var_dump(bindec("0011"));
// int(3)

var_dump(bindec(1010));
// int(10)

ceil() Round value up - accepts an integer or floating-point value as its argument and returns the next highest integer value (as a float) by rounding the value up if necessary. For example:

var_dump(ceil(11.25));
// float(12)

var_dump(ceil(-7.33));
// float(-7)

cos() Cosine - accepts a floating-point value representing an angle in radians as its argument, and returns the cosine of that angle. For example:

var_dump(cos(M_PI));
// float(-1)

var_dump(cos(M_PI/4));
// float(0.7071067811865476)

cosh() Hyperbolic cosine - accepts a floating-point value as its argument, and returns the hyperbolic cosine for that value. For example:

var_dump(cosh(2));
// float(3.7621956910836314)

var_dump(cosh(1));
// float(1.5430806348152437)

var_dump(cosh(0));
// float(1)

var_dump(cosh(-1));
float(1.5430806348152437)

decbin() Decimal to binary conversion - accepts an unsigned integer value as its argument and returns the binary representation of that value. Leading zeros are not displayed (if negative values are passed to decbin(), its behaviour is undefined).For example:

var_dump(decbin(25));
// string(5) "11001"

dechex() Decimal to hexadecimal conversion - accepts an unsigned integer value as its argument and returns the hexadecimal representation of that value (if negative values are passed to dechex(), its behaviour is undefined). For example:

var_dump(dechex(685));
// string(3) "2ad"

decoct() Decimal to octal conversion - accepts an unsigned integer value as its argument and returns the octal representation of that value (if negative values are passed to decoct(), its behaviour is undefined). For example:

var_dump(decoct(276));
// string(3) "424"

deg2rad() Degrees to radians conversion - accepts an argument representing degrees as a floating-point value and returns the equivalent number of radians as a floating-point value. For example:

var_dump(deg2rad(135));
// float(2.356194490192345)

var_dump(deg2rad(180));
// float(3.141592653589793)

exp() Calculate e to a power - accepts a floating-point value representing an exponent as its argument, and returns the value of e (the base of the natural system of logarithms, or approximately 2.718282 - also known as Euler’s number) raised to the power of that exponent as a floating-point value. For example:

var_dump(exp(2));
// float(7.38905609893065)

var_dump(exp(-1));
// float(0.36787944117144233)

expm1() Calculate (e to a power) - 1 - accepts a floating-point value representing an exponent as its argument, and returns a value one less than e (the base of the natural system of logarithms, or approximately 2.718282 - also known as Euler’s number) raised to the power of that exponent, as a floating-point value. For example:

var_dump(expm1(2));
// float(6.38905609893065)

var_dump(expm1(-1));
// float(-0.6321205588285577)

fdiv() Divide two numbers - accepts two floating-point values as its arguments, and returns the result of dividing the first argument (the dividend or numerator) by the second argument (the divisor), as a floating-point number. For example:

var_dump(fdiv(15, 2.5));
// float(6)

var_dump(fdiv(-5, 2));
// float(-2.5)

floor() Round values down - accepts an integer or floating-point value as its argument and returns the next smallest integer value (as a float) by rounding the value down if necessary. For example:

var_dump(floor(9.85));
// float(9)

var_dump(floor(-12.35));
// float(-13)

fmod() Floating-point modulus - accepts two floating-point values as its arguments, and returns the floating-point remainder (modulus) that is left after dividing the first argument (the dividend or numerator) by the second argument (the divisor). For example:

var_dump(fmod(5.7, 1.3));
// float(0.5)

var_dump(fmod(21.5, 6));
// float(3.5)

hexdec() Hexadecimal to decimal conversion - accepts the string representation of the hexadecimal number to be converted, which must be a positive integer value, as its argument. Quotes must be used if the hexadecimal number contains any non-numeric charachters (a-f or A-F). Returns the value as a base 10 integer. Note: invalid characters generate a deprecation notice and will be ignored. The result will be calculated as if the invalid characters did not exist. For example:

var_dump(hexdec("f8"));
// int(248)

var_dump(hexdec(100));
// int(256)

var_dump(hexdec("fff0"));
// int(65520)

var_dump(hexdec("FREE"));
// Deprecated: Invalid characters passed...
// int(4078)

hypot() Length of hypotenuse - accepts two floating-point values representing the lengths of the opposite and adjacent sides of a right-angled triangle (or x and y coordinates) as its arguments, and returns the length of the hypotenuse (or the distance of a point x, y from the origin) as a floating-point number. For example:

var_dump(hypot(3, 4));
// float(5)

var_dump(hypot(7, 9));
// float(11.40175425099138)

intdiv() Integer division - accepts two integer values as its arguments, and returns the integer result of dividing the first argument (the dividend or numerator) by the second argument (the divisor). Any remainder is discarded. For example:

var_dump(intdiv(3, 4));
// int(0)

var_dump(intdiv(15, 7));
// int(2)

var_dump(intdiv(18, -5));
// int(-3)

is_finite() Is finite - accepts a floating-point value as its argument and returns true or false, depending on whether or not the value is finite. For example:

var_dump(is_finite(PHP_FLOAT_MAX));
// bool(true)

var_dump(is_finite(PHP_FLOAT_MAX * 1.1));
// bool(false)

is_infinite() Is infinite - accepts a floating-point value as its argument and returns true or false, depending on whether or not the value is infinite. For example:

var_dump(is_infinite(PHP_FLOAT_MAX));
// bool(false)

var_dump(is_infinite(PHP_FLOAT_MAX * 1.1));
// bool(true)

is_nan() Is not a number - accepts a floating-point value as its argument and returns true or false, depending on whether or not the value is a number. For example:

var_dump(is_nan(sqrt(-1)));
// bool(true)

var_dump(is_nan("123"));
// bool(false)

var_dump(is_nan(INF));
// bool(false)

log() Logarithm - accepts two floating-point values as its arguments. The first argument specifies a number ($num), the second (optional) argument specifies a base ($base). If $base is specified, log() returns the log$base  $num as a float, otherwise it returns loge  $num (i.e. the natural logarithm of $num) as a float. If a negative value is supplied as the argument, log() returns NAN. For example:

var_dump(log(120));
// float(4.787491742782046)

var_dump(log(120, 16));
// float(1.7267226489021297)

var_dump(log(-10));
// float(NAN)

log10() Logarithm to base 10 - accepts a floating-point value as its argument and returns the logarithm to base 10 of that value as a float. If a negative value is supplied as the argument, log10() returns NAN. For example:

var_dump(log10(25));
// float(1.3979400086720377)

var_dump(log10(-12));
// float(NAN)

log1p() Logarithm of one plus number - accepts a floating-point value as its argument and returns the logarithm of one plus that value and returns the natural logarithm (i.e. log to base e) of one plus that value as a float. If a negative value is supplied as the argument, log1P() returns NAN. For example:

var_dump(log1p(15));
// float(2.772588722239781)

var_dump(log1p(-10));
// float(NAN)

max() Maximum value - accepts a list of two or more mixed-type values, any of which can be an array, and returns the argument with the highest value. If the only parameter is an array, max() returns the highest value in the array. If at least two parameters are provided, max() returns the greatest of these values.

Values of different types are compared using standard comparison rules. Multiple non-numeric string values are compared alpha-numerically. The winning argument is returned as received, with no type conversion.

For example:

var_dump(max(5, 11, 9, 13, 7, 3));
// int(13)

$fruit = ["apple", "orange", "banana", "mango", "cherry"];
var_dump(max($fruit));
// string(6) "orange"

echo "
";
var_dump(max(25.9, 18.7));
// float(25.9)

var_dump(max(7, "eight", "4", "9.5"));
// string(5) "eight"

min() Minimum value - accepts a list of two or more mixed-type values, any of which can be an array, and returns the argument with the lowest value. If the only parameter is an array, min() returns the lowest value in the array. If at least two parameters are provided, min() returns the smallest of these values.

Values of different types are compared using standard comparison rules. Multiple non-numeric string values are compared alpha-numerically. The winning argument is returned as received, with no type conversion.

For example:

$nums = [21, 35, 2, 18, 63];
var_dump(min(5, 11, 9, 13, 7, 3));
// int(3)

$fruit = ["apple", "orange", "banana", "mango", "cherry"];
var_dump(min($fruit));
// string(5) "apple"

var_dump(min(25.9, 18.7));
// float(18.7)

var_dump(min(7, "eight", "4", "9.5"));
// string(1) "4"

octdec() Octal to decimal conversion - accepts the string representation of the octal number to be converted, which must be a positive integer value, as its argument. Returns the value as a base 10 integer. Note: invalid characters generate a deprecation notice and will be ignored. The result will be calculated as if the invalid characters did not exist. For example:

var_dump(octdec("77"));
// int(63)

var_dump(octdec(100));
// int(64)

var_dump(octdec("F25"));
// Deprecated: Invalid characters passed ...
// int(21)

pi() Get value of pi - returns an approximation of pi as a floating-point number. For example:

$pi = pi();
var_dump($pi);
// float(3.141592653589793)

pow() Exponential - accepts two numeric values as arguments, and returns the result of raising the first value to the power of the second. Each argument can be either an integer or a floating-point value. If both arguments are non-negative integers, and if the result can be represented as an integer, the return value will be an integer. Otherwise, it will be a float. For example:

var_dump(pow(2,2));
// int(4)

var_dump(pow(10,-1));
// float(0.1)

var_dump(pow(10,0.5));
// float(3.1622776601683795)

var_dump(pow(2.5, 2));
// float(6.25)

rad2deg() Radians to degrees conversion - accepts an argument representing radians as a floating-point value and returns the equivalent number of degrees as a floating-point value. For example:

var_dump(rad2deg(M_PI));
// float(180)

var_dump(rad2deg(M_PI_4));
// float(45)

round() Round a number up or down - accepts three numeric arguments. The first argument is the integer or floating-point value to be rounded. The second (optional) argument is the precision to which the number is rounded, and can be a positive or negative integer. The third (optional) argument specifies the rounding mode. The result is always returned as a floating-point value, even if it is an integer.

If neither of the optional arguments is used, the value passed to round() is rounded up or down to the nearest integer value. By default, fractional components of 0.5 or greater are always rounded away from zero. Fractional components of less than 0.5 are always rounded towards zero. For example:

var_dump(round(2.499));
// float(2)

var_dump(round(2.5));
// float(3)

var_dump(round(-2.499));
// float(-2)

var_dump(round(-2.5));
// float(-3)

The optional second argument specifies the precision to which the floating-point argument is rounded. For example:

var_dump(round(2.4899, 2));
// float(2.49)

var_dump(round(2.5432, 2));
// float(2.54)

var_dump(round(-2.4899, 2));
// float(-2.49)

var_dump(round(-2.5578, 2));
// float(-2.56)

The value given for precision can also be negative, which allows us to round the integer part of a floating-point value to the nearest multiple of 10-precision. For example:

var_dump(round(9994, -1));
// float(9990)

var_dump(round(9995, -1));
// float(10000)

var_dump(round(2355, -2));
// float(2400)

var_dump(round(-2355, -2));
// float(-2400)

The optional rounding mode argument, if used, specifies whether the number is rounded away from or towards zero, to the nearest even number, or to the nearest odd number, using one of the following constants:

PHP_ROUND_HALF_UP - if the fractional component of a number is equal to or greater than 0.5, rounds the number away from zero. This is the default behaviour. For example:

var_dump(round(2.49, 0, PHP_ROUND_HALF_UP));
// float(2)

var_dump(round(2.5, 0, PHP_ROUND_HALF_UP));
// float(3)

PHP_ROUND_HALF_DOWN - if the fractional component of a number is equal to or less than 0.5, rounds the number towards zero. For example:

var_dump(round(2.51, 0, PHP_ROUND_HALF_DOWN));
// float(3)

var_dump(round(2.5, 0, PHP_ROUND_HALF_DOWN));
// float(2)

PHP_ROUND_HALF_EVEN - if the fractional component of a number is equal to 0.5, rounds the number towards the nearest even value. For example:

var_dump(round(3.5, 0, PHP_ROUND_HALF_EVEN));
// float(4)

var_dump(round(4.5, 0, PHP_ROUND_HALF_EVEN));
// float(4)

PHP_ROUND_HALF_ODD - if the fractional component of a number is equal to 0.5, rounds the number towards the nearest odd value. For example:

var_dump(round(3.5, 0, PHP_ROUND_HALF_ODD));
// float(3)

var_dump(round(4.5, 0, PHP_ROUND_HALF_ODD));
// float(5)

sin() Sine - accepts a floating-point value representing an angle in radians as its argument, and returns the sine of that angle. For example:

var_dump(sin(M_PI/3));
// float(0.8660254037844386)

var_dump(sin(M_PI_4));
// float(0.7071067811865476)

sinh() Hyperbolic sine - accepts a floating-point value as its argument, and returns the hyperbolic sine for that value. For example:

var_dump(sinh(2));
// float(3.6268604078470186)

var_dump(sinh(1));
// float(1.1752011936438014)

var_dump(sinh(-1));
// float(-1.1752011936438014)

var_dump(sinh(-2));
// float(-3.6268604078470186)

sqrt() Square root - accepts a floating-point value as its argument and returns the square root of that number as a float. Returns NAN if the argument is a negative value. For example:

var_dump(sqrt(25));
// float(5)

var_dump(sqrt(15));
// float(3.872983346207417)

var_dump(sqrt(2.5));
// float(1.5811388300841898)

var_dump(sqrt(-16));
// float(NAN)

tan() Tangent - accepts a floating-point value representing an angle in radians as its argument, and returns the tangent of that angle. For example:

var_dump(tan(M_PI/4));
// float(0.9999999999999999)

var_dump(tan(M_PI/3));
// float(1.7320508075688767)

tanh() Hyperbolic tangent - accepts a floating-point value as its argument, and returns the hyperbolic tangent for that value. For example:

var_dump(tanh(-1));
// float(-0.7615941559557649)

var_dump(tanh(1));
// float(0.7615941559557649)

var_dump(tanh(10));
// float(0.9999999958776927)