PHP Math

PHP Math functions allow you to perform mathematical operations on numbers.

PHP Math Functions:

There are several built-in functions in PHP that can help you with anything from basic addition and subtraction to complex calculations.

PHP abs() Function:

The abs() function is a PHP built-in function that returns the absolute (positive) value of a number. The abs() function in PHP is the same as the modulus function in mathematics, It makes the number positive.

echo(abs(-6)). "</br>";//Integer
echo(abs(-5.5)). "</br>";//Float
echo(abs(6)). "</br>";//It does not change positive value

PHP sqrt() Function:

To find the square root of a positive number, the sqrt() function is used. For negative numbers, this function returns the special value NAN.

echo(sqrt(9) . "<br>");//3
echo(sqrt(10) . "<br>");//3.1622776601684
echo(sqrt(81) . "<br>");//9
echo(sqrt(-81));// NAN

PHP pi() Function:

pi() function returns the approximation value of pi.

echo("Value of PI is ".pi());

PHP rand() Function

PHP rand() function is used to produce a random number. By passing the min and max arguments, you can optionally define a range.

echo(rand() ."</br>");
echo(rand(10, 100));// Random number between 10 to 100

PHP round() Function

A floating-point number is rounded to the nearest integer using the round() function.

echo(round(0.20) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.69) . "<br>");
echo(round(-4.20) . "<br>");

PHP min() and max() Functions

To find the lowest or highest value in a list of arguments, min() and max() function is used.

echo(min(10, 15,250, -15, -20) . "<br>");
echo(max(10, 15,250, -15, -20));