Back to Blog Page
Backend Development

What are Math Functions in PHP?

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:

1
$myNegativeNum = -40;
2
echo abs($myNegativeNum); // 40
3
4
5
echo abs(-120); // 120;

The rand() Function

The rand() function generates a random integer of any number of figures depending on your system configuration:

1
echo rand(); //15059801

If you keep reloading the server, you’ll keep getting different numbers:

1
echo rand(); //1495051557

To see the maximum value that rand() can return, you can use getrandmax(). For me, that figure is 2147483647:

1
echo getrandmax(); // 2147483647

If you pass in two numbers into the rand() function, it will generate the random number between those numbers:

1
//syntax: rand(min, max);
2
echo rand(1, 10); //6
3
echo rand(1, 10); //5
4
echo rand(1, 10); //9

The floor() Function

The floor function is used to “round down” a floating-point number to the nearest whole number (integer):

1
echo floor(43.2); // 43

Even if the number is closer to the next integer, it would still be rounded down:

1
echo ceil(43.9); // 43

The ceil() Function

The ceil function is the opposite of floor(). It rounds up a floating-point number to the nearest whole number:

1
echo ceil(43.9); // 44

And if the number is closer to the whole number before it, it would still be rounded up:

1
echo ceil(43.3); // 44

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:

1
echo round(2.3); // 2

And if the number is closer to the next whole number (integer), it would be rounded up:

1
echo round(1.8); // 2

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:

1
$base = 2;
2
$exponent = 4;
3
4
5
echo pow($base, $exponent); // 16

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:

1
echo sqrt(16); // 4
2
echo sqrt(64); // 8
3
echo sqrt(81); // 9

The min() Function

The min function returns the minimum number within the numbers passed into it.

1
echo min(2, 4, 1, 5); // 1

This means a single argument is invalid in it:

1
echo min(1);

Math.min() error

1
$myNumArr = [3, 6, 9, 2];
2
echo min($myNumArr); // 2

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().

1
echo max(2, 4, 1, 5); // 5

You can use it to find the highest number in an array of numbers:

1
$myNumArr = [3, 6, 9, 2];
2
echo max($myNumArr); // 9

max() will also return an error if only one number is passed into it:

1
echo max(9);

Math.max() error

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:

1
echo pi(); // 3.1415926535898

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