W3docs

Understanding PHP Math Functions

PHP provides a range of mathematical functions that allow developers to perform various mathematical operations in their code. These functions can be used to

PHP provides a range of built-in mathematical functions that let you perform calculations without writing the underlying logic yourself. These functions work on numbers and data types such as integers and floats, and they complement the arithmetic operators (+, -, *, /, %) you already know. This article walks through the most commonly used PHP math functions — rounding, trigonometry, logarithms, powers, and random numbers — with runnable examples for each.

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 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 example of ceil and floor function usage

<?php

$num = 3.14159;

$ceiled = ceil($num);
echo $ceiled;
// Output: 4

echo "\n";

$floored = floor($num);
// Output: 3
echo $floored;

?>

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.

Important: All PHP trigonometric functions expect angles in radians. Use deg2rad() to convert degrees to radians before passing them to these functions.

PHP example of math functions usage deg2rad, sin, cos and tan

<?php

$angle = deg2rad(30);
echo $angle . "\n";

$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 example of Logarithmic Functions usage

<?php

$num = 10;

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

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

$exponential = exp($num);
print  $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. You can also use sqrt for square roots, and max/min to find the largest or smallest value in a set.

PHP example of mathematical function usage pow and abs

<?php

$num = -5;

$absolute = abs($num);
print $absolute . "\n";
// Output: 5

$power = pow(2, 3);
print $power . "\n";
// Output: 8

$square_root = sqrt(16);
print $square_root . "\n";
// Output: 4

$maximum = max(1, 5, 3);
print $maximum . "\n";
// Output: 5

$minimum = min(1, 5, 3);
print $minimum . "\n";
// Output: 1

?>

Generating Random Numbers

Random numbers are a common need — for tokens, sampling, or games. Use rand() (or the faster, more uniform mt_rand()) to get a random integer within a range, and random_int() when you need cryptographically secure values:

<?php

$dice = rand(1, 6);
echo $dice . "\n";
// Output: a number between 1 and 6, e.g. 4

$secure = random_int(1, 100);
echo $secure . "\n";
// Output: a cryptographically secure number between 1 and 100

?>

Note: there is no random() function in PHP — the correct names are rand(), mt_rand(), and random_int().

Conclusion

In this article, we have explored some of the most commonly used PHP math functions and how they can be used to perform various mathematical operations. Whether you are rounding numbers, performing trigonometric calculations, or working with logarithmic functions, PHP provides a range of functions to help you get the job done. With a little bit of practice, you can leverage these built-in functions to handle complex numerical tasks efficiently in your PHP applications. To go deeper, see the full PHP Math reference and the chapter on formatting numbers for display.

Quick Reference Table

FunctionSignatureReturn TypeExample
round()round(float $num, int $precision = 0): floatfloatround(3.14159, 2)3.14
ceil() / floor()ceil(float $num): float / floor(float $num): floatfloatceil(3.14)4.0
sin() / cos() / tan()sin(float $num): float (etc.)floatsin(deg2rad(30))0.5
log() / log10() / exp()log(float $num, float $base = M_E): float (etc.)floatlog(10)2.30259
abs()`abs(intfloat $num): intfloat`
pow()pow(float $num, float $exp): floatfloatpow(2, 3)8.0
sqrt()sqrt(float $num): floatfloatsqrt(16)4.0
max() / min()max(mixed $value, mixed ...$values): mixedmixedmax(1, 5, 3)5
rand() / mt_rand()rand(int $min, int $max): intintrand(1, 6) → e.g. 4
random_int()random_int(int $min, int $max): intintrandom_int(1, 100) → secure int

Practice

Practice
Which of the following functions are used in PHP for mathematical operations?
Which of the following functions are used in PHP for mathematical operations?
Was this page helpful?