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 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:
|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:
Below is an example for finding area of circle using pi( π ) function:
- First we have defined a variable $r and assigns it a value of 3, which represents the radius of the circle.
- 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.
- The result of this calculation is stored in a variable called $area_of_circle.
- 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:
This example defines an array $a with 5 elements, including numbers of different types and a string:
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:
In the following example, if you pass a string parameter to abs(), it returns 0:
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:
In another example -9 is not defined, so it 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.
- $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):
We can also set the decimal points as shown in the below example:
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:
In the following code example, you can see that every time we call the rand() function we get a different random value:
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):
Another example shows a random number between 50 and 100: