Skip to content

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 perform calculations on numbers, manipulate floating-point values, and even round numbers to specific decimal places. In this article, we will be exploring some of the most commonly used PHP math functions and how they can be used to perform various mathematical operations.

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
<?php

$num = 3.14159;
$rounded = round($num, 2);

print $rounded;

// Output: 3.14

?>

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
<?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
<?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
<?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
<?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

?>

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.

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

Practice

Which of the following functions are used in PHP for mathematical operations?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.