W3docs

Understanding PHP Math Functions

Guide to PHP math functions: rounding, powers, roots, min/max, trigonometry, logarithms, random numbers, and built-in constants.

PHP ships with a large set of built-in math functions that let you perform calculations beyond what the basic arithmetic operators (+ - * / %) can do on their own. With them you can round floating-point values, take square roots and powers, work with trigonometry and logarithms, and generate random numbers — all without installing any extension. Every function shown here is part of PHP core.

This chapter walks through the functions you will reach for most often, grouped by what they do, with a short note on when each one is useful. If you are new to numeric values in PHP, read PHP Numbers first.

Operators vs. Functions

For everyday arithmetic, use operators — they are shorter and faster to read:

<?php
echo 7 + 3;   // 10
echo 7 % 3;   // 1  (modulo: remainder)
echo 2 ** 10; // 1024 (exponentiation operator)
?>

Reach for the math functions when you need something operators cannot express on their own — rounding, square roots, absolute values, trigonometry, logarithms, or random numbers. The ** operator and pow() do the same job; the rest of this page covers cases that have no operator equivalent.

Rounding Functions

One of the most common mathematical operations performed in PHP is rounding numbers to a specific decimal place. The round function can be used to round a number to the nearest integer or to a specific number of decimal places. For example, the following code will round the number 3.14159 to two decimal places:

PHP math round function example

php— editable, runs on the server

In addition to the round function, PHP also provides the ceil and floor functions, which can be used to round a number up or down to the nearest integer. For example:

PHP ceil and floor function examples

php— editable, runs on the server

When to use which: round() rounds to the nearest value (and accepts a precision), ceil() always rounds up, and floor() always rounds down. Note that ceil() and floor() return a float, so ceil(3.1) is 4.0, not the integer 4. A common gotcha is that round() uses "round half away from zero" by default, so round(2.5) is 3 and round(-2.5) is -3.

Powers and Roots

Use pow() (or the ** operator) to raise a number to a power, and sqrt() to take a square root. abs() returns the absolute (non-negative) value, which is handy when you only care about magnitude — for example, the distance between two numbers.

PHP pow, sqrt and abs example

<?php

echo pow(2, 8) . "\n";  // 256
echo 2 ** 8 . "\n";     // 256 (operator form)
echo sqrt(144) . "\n";  // 12
echo abs(-17) . "\n";   // 17

// Distance between two numbers, regardless of order:
echo abs(3 - 10) . "\n"; // 7
?>

Min, Max and Sums

min() and max() return the smallest and largest of their arguments. You can pass them a list of values or a single array. array_sum() and array_product() add up or multiply all elements of an array.

PHP min, max and array_sum example

<?php

echo min(4, 2, 9, 1) . "\n";          // 1
echo max([4, 2, 9, 1]) . "\n";        // 9 (works on an array too)

$prices = [19.99, 5.50, 12.00];
echo array_sum($prices) . "\n";       // 37.49
echo array_product([2, 3, 4]) . "\n"; // 24
?>

Trigonometric Functions

PHP provides several trigonometric functions, including sin, cos, and tan, which can be used to calculate the sine, cosine, and tangent of an angle, respectively. These functions can be useful for performing calculations related to angles and distances in 2D and 3D space. Note that these functions expect angles in radians, not degrees. Use deg2rad() to convert degrees to radians before passing them to these functions.

PHP sin, cos and tan example

<?php

$angle = deg2rad(30);

$sine = sin($angle);
echo $sine . "\n";
// Output: 0.5

$cosine = cos($angle);
echo $cosine . "\n";
// Output: 0.866025

$tangent = tan($angle);
echo $tangent . "\n";
// Output: 0.577350

?>

Logarithmic Functions

PHP provides several logarithmic functions, including log, log10, and exp, which can be used to perform various logarithmic operations. The log function calculates the natural logarithm of a number, while the log10 function calculates the logarithm base 10 of a number. The exp function calculates the exponential value of a number.

PHP log, log10 and exp functions example

<?php

$num = 10;

$natural_log = log($num);
echo $natural_log . "\n";
// Output: 2.30259

$log10 = log10($num);
echo $log10 . "\n";
// Output: 1

$exponential = exp($num);
echo $exponential . "\n";
// Output: 22026.465
?>

Miscellaneous Functions

In addition to the functions discussed above, PHP provides several other mathematical functions that can be used for various purposes. For example, the abs function can be used to calculate the absolute value of a number, while the pow function can be used to calculate the power of a number.

PHP abs and pow function example

php— editable, runs on the server

For integer-only division, use intdiv(), and for the floating-point remainder, use fmod():

<?php
echo intdiv(17, 5) . "\n"; // 3  (integer division)
echo fmod(17.5, 5) . "\n"; // 2.5 (float remainder; % only works on ints)
?>

Random Numbers

To generate random numbers, use rand() or mt_rand() for general purposes, and random_int() when the value must be cryptographically secure (passwords, tokens, OTP codes). All three accept an inclusive minimum and maximum.

PHP random number example

<?php

$dice = rand(1, 6);          // a number from 1 to 6
echo $dice . "\n";

$secure = random_int(1, 100); // cryptographically secure 1–100
echo $secure . "\n";
?>

Because the output changes on every run, the exact numbers above will differ each time. Use random_int() whenever the randomness is security-sensitive — rand() is predictable and must not be used for anything secret.

Math Constants

PHP defines several built-in constants for common values, so you do not have to hard-code approximations:

<?php
echo M_PI . "\n";   // 3.1415926535898  (π)
echo M_E . "\n";    // 2.718281828459   (Euler's number)
echo M_SQRT2 . "\n";// 1.4142135623731  (√2)

// Area of a circle with radius 5:
echo M_PI * pow(5, 2) . "\n"; // 78.539816339745
?>

The constant PHP_INT_MAX tells you the largest integer your platform can hold; exceed it and PHP silently converts the result to a float. See PHP Numbers and PHP Data Types for more on integer and float limits.

Conclusion

PHP's math toolkit covers everything from simple rounding to trigonometry, logarithms, and secure random numbers — all built into the language with no extension required. As a rule of thumb: use operators for plain arithmetic, round/ceil/floor to control precision, min/max/array_sum to summarise collections, and random_int() whenever randomness must be unpredictable. To keep formatting numbers for display separate from calculating them, see number_format.

Practice

Practice
In PHP, what functions are used to find minimum and maximum values among given numbers?
In PHP, what functions are used to find minimum and maximum values among given numbers?
Was this page helpful?