Math Function In PHP

In this article, we will look at PHP math functions and demonstrate how they can be used to solve problems.

The PHP language has a set of math functions that let you perform mathematical operations on numbers in a simple, easy-to-use way.

PHP Math Functions

PHP Math

PHP Math refers to the ability to perform arithmetic operations such as addition, subtraction, multiplication, and division. In addition, it can perform more complex mathematical functions such as calculating the square root, logarithm, sine, cosine, and tangent of a number.

Depending on your needs, you can use these functions for basic arithmetic or more advanced calculations.

Following are some of the PHP math functions:

Math FunctionsOverview
pi()Returns the absolute value of a number.
min()Rounds a number to the nearest integer.
max()Rounds a number up to the nearest integer.
abs()Rounds a number down to the nearest integer.
sqrt()Calculates the square root of a number.
round()Raises a number to a given power.

PHP pi() Function

The pi() function is a built-in PHP math function that returns the value of the mathematical constant pi ( π ).

The value of Pi is an irrational number equal to the length of 3.1415926535898, approximately.

There are no arguments required for the pi() function and it is very easy to use. Here’s an example:


<?php echo(pi()); // returns 3.1415926535898 ?>

Below is an example for finding area of circle using pi( π ) function:


<?php // for area of circle $r = 3; //radius $area_of_circle = (pi()*($r*$r)); //formula of area of circle echo($area_of_circle); // returns 28.274333882308 ?>

Example Explanation:

  1. First we have defined a variable $r and assigns it a value of 3, which represents the radius of the circle.
  2. Then we calculate the area of the circle using the formula (pi()*($r* $r)) where pi() is a built-in PHP math function that returns the mathematical constant π and $r * $r calculates the square of the radius.
  3. The result of this calculation is stored in a variable called $area_of_circle.
  4. Finally, we have used the echo statement to display the value of $area_of_circle on the screen.

The output of the code is the result of the area calculation, which is approximately 28.274333882308.

PHP min() and max() Functions

A min() and max() function is another mathematics function that is useful in finding the minimum and maximum values of an array of numbers or a set of numbers in a list.

When working with numerical data in PHP, these functions are very helpful and will save you time and effort.

Following are examples of using the min() and max() functions:


<?php echo(min(2, 78, 32, 42, -100, -3)); // returns -100 echo(max(2, 78, 32, 42, -100, -3)); // returns 78 ?>

This example defines an array $a with 5 elements, including numbers of different types and a string:


<?php $a = array(9.2, 8, 5, "2", 6); echo("Min value: ". min($a) . "<br>"); // returns 2 echo("Max value: ". max($a)); // returns 9.2 ?>

A default argument value is a value assigned to a function parameter that is used when the argument is not explicitly passed to the function.

This allows a function to be called with fewer arguments than it requires, and provides a default value for any missing arguments.

The min($a) function returns the smallest value in the array. Since “2” is a string, it is automatically converted to a number before the minimum value is calculated, resulting in 2.

Max($a) returns the largest value in the array as a number. As output, we get 9.2, which is a float.

PHP Math abs() Function

Abs() returns the absolute value of a number. In mathematical terms, the absolute value of a number is its distance from zero, no matter what the number is, whether it is positive or negative.

As an example, the absolute value of -6 is 6, and the absolute value of 6 is also 6:


<?php echo(abs(-6)); // returns 6 ?>

In the following example, if you pass a string parameter to abs(), it returns 0:


<?php $num="Hello"; echo "String: " . $num . ""; echo "absolute value : " . abs($num) . ""; ?>

PHP Math sqrt() Function

sqrt() function gives the square root of a number. The square root is the number multiplied by itself that equals the original number.

For example, the square root of 9 is 3 because 3 times 3 equals 9:


<?php echo(sqrt(9)); // returns 3 ?>

In another example -9 is not defined, so it returns NaN:


<?php echo(sqrt(-9)); // returns NaN ?>

PHP Math round() Function

Rounding floats in PHP is achieved by round().

It takes two arguments: the number to be rounded and another optional argument specifying how many decimals to round.


round($number, $precision)

Syntax Explanation:

  • $number : This is required. Round the number.
  • $precision : This is optional. To round how many decimal places. By default, it’s 0.

In the following example, the round() function rounds the value to floor and ceiling values (example: ceiling is a low value & floor is a large value):


<?php echo(round(0.51)); // returns 1 echo(round(0.48));// returns 0 ?>

We can also set the decimal points as shown in the below example:


<?php echo(round(2.37454,3)); // returns 2.374 echo(round(2.68124, 2));// returns 2.68 ?>


PHP Math Rand() Function

In order to generate a random integer, we can use the rand() function.

Below is an example showing that rand() gives a random number:


<?php echo(rand()); ?>

In the following code example, you can see that every time we call the rand() function we get a different random value:


<?php $a = rand(); echo $a . "<br>"; echo rand()."<br>"; print rand(); ?>

There are optional parameters $max and $min that you can use to specify the lowest integer and the highest integer that will be returned for the random number in order to gain more control over the random number.

As an example, if you want a random integer between 1 and 10 (inclusive), then you would use rand(1, 10):


<?php echo(rand(1, 10)); ?>

Another example shows a random number between 50 and 100:


<?php echo(rand(50, 100)); ?>
We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *