Advertisement
Google Ad Slot: content-top
PHP Math
There are various built-in functions available for performing mathematical operations. Here's an overview of some of the common PHP math functions:
Function |
Description |
Example |
|---|---|---|
pi() |
Returns the value of π (Pi), which is approximately 3.1415926535898 |
echo(pi()) |
min() and max() |
Returns the smallest or largest value in an array or a set of values |
echo min(1, 2, 3, 4); // 1 echo max(1, 2, 3, 4); // 4 |
abs() |
Returns the absolute value of a number |
echo abs(-10); // 10 |
pow() |
Returns the base raised to the power of the exponent |
echo pow(2, 3); // 8 |
sqrt() |
Returns the square root of a number |
echo sqrt(16); // 4 |
round() |
Rounds a floating-point number |
echo round(3.1459, 2); // 3.15 |
floor() |
Rounds a number down to the nearest integer |
echo floor(3.9); // 3 |
ceil() |
Rounds a number up to the nearest integer |
echo ceil(3.1); // 4 |
rand() |
Function generates a random number |
echo rand(); // Random number |
rand() function with parameters:
However, you can also specify a range for the random number by passing two arguments: a minimum and maximum value.
Syntax
rand(int $min = 0, int $max = getrandmax())
- $min: The minimum number in the range (inclusive). If not provided, it defaults to 0.
- $max: The maximum number in the range (inclusive). If not provided, it defaults to
getrandmax()(the largest possible random value).