What are Math Functions in PHP?
Written by Kolade Chris | Nov 2, 2024 | #PHP | 3 minute Read
PHP, like every other programming language, allows you to perform mathematical calculations and provides math
functions for manipulating numbers.
In this article, we’ll examine 10 PHP math
functions, what they do, and how to use them.
Here are those PHP math
functions we’ll look at:
abs()
rand()
ceil()
floor()
round()
pow()
sqrt()
min()
max()
pi
number_format()
The abs()
Function
“abs” in the abs()
function means “absolute”. This means it returns the positive value of a number.
To use the abs()
function on a number, you pass the number into it:
The rand()
Function
The rand()
function generates a random integer of any number of figures depending on your system configuration:
If you keep reloading the server, you’ll keep getting different numbers:
To see the maximum value that rand()
can return, you can use getrandmax()
. For me, that figure is 2147483647
:
If you pass in two numbers into the rand()
function, it will generate the random number between those numbers:
The floor()
Function
The floor
function is used to “round down” a floating-point number to the nearest whole number (integer):
Even if the number is closer to the next integer, it would still be rounded down:
The ceil()
Function
The ceil
function is the opposite of floor()
. It rounds up a floating-point number to the nearest whole number:
And if the number is closer to the whole number before it, it would still be rounded up:
The round()
Function
The round()
function rounds a floating-point number to the nearest whole number. It’s a hybrid of the ceil()
and floor()
functions.
If the number is closer to the whole number (integer) before it, it would be rounded down:
And if the number is closer to the next whole number (integer), it would be rounded up:
The pow()
Function
The pow
function takes two required numbers and then raises the first number to the power of the second.
See these two numbers as base
and exponent
. Here’s an example:
The sqrt()
Function
sqrt
stands for square root. So, if you want to get the square root of any number, pass in that number into the sqrt()
function:
The min()
Function
The min
function returns the minimum number within the numbers passed into it.
This means a single argument is invalid in it:
The max()
Function
The max()
function finds the highest number within the numbers passed into it. Apart from that, everything about min()
is applied to max()
.
You can use it to find the highest number in an array of numbers:
max()
will also return an error if only one number is passed into it:
You probably learned about pi
during your elementary (or primary) school days. You’re not mistaken. It’s the same pi
.
This function does not need anything to be passed into it, it returns the pi
number:
The number_format()
Function
Wrapping Up
Understanding the PHP math
functions is critical to making efficient web apps that handle calculations and other math-related operations.
Apart from the math
functions discussed in this article, there are many others – including those that handle trigonometric calculations such as sin
, tan
, and cos
.
You can check those functions in the official PHP documentation for the Math functions.
Check out my other PHP articles