PHP provides a rich set of built-in math functions that allow us to perform complex mathematical calculations easily without writing complex logic manually.
These pre-defined functions are part of PHP’s core, so you don’t need to install any extra library. They are fast and easy to use in the PHP program.
Types of PHP Math Functions
PHP provides a wide variety of math functions that we can categorize as follows:
- Basic Math Functions
- Rounding Functions
- Random Number Functions
- Exponential and Logarithmic Functions
- Trigonometric Functions
- Formatting Functions
- Advanced Practical Applications
Let’s understand each one by one with examples.
Basic PHP Math Functions
We use the basic math functions in PHP for simple calculations such as finding absolute values, maximum/minimum numbers, powers, and square roots. These functions are commonly used in almost every PHP application.
1. abs() Function
The abs() function returns the absolute (positive) value of a number. The general syntax of this function is:
abs(number)
Example:
<?php
$num = -25;
echo abs($num);
?>Output:
25
In this example, the abs() function converts the negative number into a positive one. If the number is already positive, it remains unchanged.
2. max() Function
The max() function returns the largest value from the several given numbers or the largest value in an array. The general syntax of this function in PHP is:
max(value1, value2, ...)
Example:
<?php
echo max(10, 50, 30, 20);
?>Output:
50
Example: With Array
<?php
$numbers = [5, 15, 25, 10];
echo max($numbers);
?>Output:
25
3. min() Function
The min() function returns the smallest number from the several given numbers or the smallest number in an array. The general syntax of this function in PHP is:
min(value1, value2, ...)
Example:
<?php
echo min(10, 6, 30, 20) . "\n";
// With Array
$numbers = [5, 15, 25, 10];
echo min($numbers);
?>Output:
6 5
4. pow() Function
The pow() function in PHP calculates the power of a number (exponentiation). It returns the base raised to the power of exponent. The general syntax of pow() function is:
pow(base, exponent)
Example:
<?php
echo(pow(3,2) . "\n");
echo(pow(-2,6) . "\n");
echo(pow(-2,-4) . "\n");
?>Output:
9 64 0.0625
5. sqrt() Function
The sqrt() function in PHP returns the square root of a number. The general syntax of sqrt() function is:
sqrt(number)
Example:
<?php
echo(sqrt(0) . "\n");
echo(sqrt(1) . "\n");
echo(sqrt(9) . "\n");
echo(sqrt(0.64));
?>Output:
0 1 3 0.8
6. pi() Function
The pi() function in PHP returns the value of π (pi). The general syntax of pi() function is:
pi()
Example:
<?php
echo pi();
?>Output:
3.1415926535898
7. intdiv() Function
The intdiv() function in PHP is used to perform integer division. The basic syntax of intdiv() function is:
intdiv(dividend, divisor)
Example:
<?php
echo intdiv(8, 4) . "\n";
echo intdiv(10, 3);
?>Output:
2 3
Note: The intdiv() function returns only the integer part and ignores the decimal part. That’s why this function is useful when decimals are not needed.
PHP Rounding Functions
Rounding functions in PHP are used to adjust decimal numbers to integers or specific precision levels. The list of important PHP rounding functions is:
- round()
- ceil()
- floor()
- intval() (basic integer conversion)
8. round() Function
The round() function in PHP rounds a floating-point number to the nearest integer or specified decimal places. The general syntax of round() function is:
round(number, precision, mode)
In the above syntax:
- The number can be an integer or a floating-point (decimal) number that you want to round.
- The precision specifies how many decimal places you want in the result. This is optional. The default value is 0 (rounds to nearest integer).
- The mode defines how rounding should behave in special cases (like .5 values). Some common mode constants are:
- PHP_ROUND_HALF_UP (default): Rounds .5 up.
- PHP_ROUND_HALF_DOWN: Rounds .5 down.
- PHP_ROUND_HALF_EVEN: Rounds to nearest even number.
- PHP_ROUND_HALF_ODD: Rounds to nearest odd number.
Example:
<?php
echo round(4.4) . "\n";
echo round(4.5) . "\n";
echo round(4.567, 2) . "\n";
echo round(2.5, 0, PHP_ROUND_HALF_UP) . "\n";
echo round(2.5, 0, PHP_ROUND_HALF_DOWN) . "\n";
echo round(2.5, 0, PHP_ROUND_HALF_EVEN) . "\n";
echo round(2.5, 0, PHP_ROUND_HALF_ODD);
?>Output:
4 5 4.57 3 2 2 3
9. ceil() Function
The ceil function in PHP rounds a number up to the nearest integer. The general syntax of ceil() function is:
ceil(number)
Example:
<?php
echo ceil(4.1) . "\n";
echo ceil(4.9) . "\n";
echo ceil(-5.1) . "\n";
echo ceil(-5.9);
?>
Output:
5 5 -5 -5
10. floor() Function
The floor() function in PHP rounds a number down to the nearest integer. The basic syntax of floor() function is:
floor(number)
Example:
<?php
echo floor(4.9) . "\n";
echo floor(4.1) . "\n";
echo floor(0.60) . "\n";
echo floor(5) . "\n";
echo floor(5.1) . "\n";
echo floor(-5.9);
?>
Output:
4 4 0 5 5 -6
11. intval() Function
The intval() function converts a value to an integer. It simply removes the decimal part from a number. The basic syntax of intval() function in PHP is:
intval(value)
Example:
<?php
echo intval(4.9) . "\n";
echo intval(4.1) . "\n";
echo intval(0.60) . "\n";
echo intval(-5.9);
?>Output:
4 4 0 -5
PHP Random Number Functions
Random number functions in PHP are used to generate unpredictable values. The list of important random number functions is:
- rand()
- mt_rand()
- random_int()
- lcg_value()
These functions are widely used in:
- OTP generation
- Password/token creation
- Games and simulations
- Random selection
12. rand() Function
The rand() function in PHP generates a random integer number within the specified range. For example, if you want to generate a random integer number between 10 and 50 (inclusive), use rand (10, 50). The general syntax of rand() function is:
rand(min, max)
In this syntax:
- The parameter min specifies the lowest number to return. Default is 0. It is optional.
- The parameter max specifies the highest number to return. Default is getrandmax(). It is optional.
Example:
<?php
echo rand(1, 10); // Random number between 1 and 10
?>Output:
9
13. mt_rand() Function
The mt_rand() function generates a random integer number using the Mersenne Twister algorithm. The general syntax of mt_rand() function is:
mt_rand(min, max)
In this syntax:
- The parameter min specifies the lowest number to return. Default is 0. It is optional.
- The parameter max specifies the highest number to return. Default is mt_getrandmax(). It is optional.
Example:
<?php
echo mt_rand() . "\n";
echo mt_rand(1, 100);
?>Output:
737210195 17
14. random_int() Function
The random_int() function in PHP is a built-in function, which is used to generate a cryptographically secure random integer. The general syntax of random_int() function is:
random_int(min, max)
Example:
<?php
echo random_int(1000, 9999);
?>Output:
8982
15. lcg_value() Function
The Icg_value() function in PHP is a built-in function that generates a pseudo-random float (decimal) number between 0 and 1. The general syntax of Icg_value() function is:
lcg_value()
Example:
<?php
echo lcg_value();
?>Output:
0.78223817459612



