# PHP Math

PHP has the Math library, which can perform various basic Mathematical operations to get the desired output.

Index

## Math Functions in PHP

In this article, we will learn how to use math Operations in PHP. The Simplest math functions are those that require only a single parameter. This parameter is usually a number, which accepts as a string or a variable. Some math functions take multiple parameters.

For complex mathematical operations, PHP math functions comes into the picture. To start using them, we do not need to install any extension or packages. PHP Math functions are useful for handling float and integer variables. These Functions are inbuilt into PHP.

In the below table, all the PHP math functions are there; if we use PHP 4 or a newer version, we can use any of them freely. Let see a detailed description of some PHP math functions.

### PHP abs() Function

The abs () is the most basic function. It is an inbuilt function in PHP, uses to return the absolute (positive) value of a number. This function accepts any number as an argument and returns a positive value. The absolute value of the name is always positive. This function always returns a positive number.

Syntax

`abs(number);`
 Parameter Description Number Numeric Value Return type Float/Integer Return value The Absolute value of a number

Example 1

```<?php

echo (abs(-6));

?>```

Output

`6`

Example 2

```<?php

echo (abs(-6));

?>```

Output

`6`

### PHP ceil() Function

The ceiling function is most widely useful in mathematical problems to round up a number to the next greater integer value. PHP provides a built-in function ceil() to perform such operation.

Syntax

`ceil(number);`
 Parameter Description Number Specifies the value to round up Return Value The value round up to the nearest integer. Return Type Float

Example 1

```<?php

echo (ceil(0.70));

?>```

Output

`1`

Example 2

```<?php

echo (ceil(-4.1));

?>```

Output

`-4`

### PHP pi() Function

The pi function is the essential function of PHP, which is useful in solving mathematical problems. The function returns the approximation value of pi. The function returns a float value 3.14159265359, which is equal to predefine constant M_PI.

Some other predefined named constants related to π are:

• M_PI_2: It describes π/2. Its value is 1.570796.
• M_PI_4: It describes π/4. Its value is 0.785398.
• M_1_PI: It describes 1/π. Its value is 0.318309.
• M_PI: It describes π. Its value is 3.141592.
• M_2_PI: It describes 2/π. Its value is 0.636619.
• M_SQRTPI: It describes sqrt(π). Its value is 1.772453.
• M_2_SQRTPI :It describes 2/sqrt(π). Its value is 1.128379.

Syntax

`pi();`
 Return Value: 3.1415926535898 Return Type: Float

Example 1

```<?php

echo(pi());

?>```

Output

`3.1415926535898`

Example 2

```<?php

echo M_PI;

?>```

Output

`3.1415926535898`

### PHP max() Function

We can calculate the maximum value among the elements of an array by using the max function. It is useful to find the maximum value in the given input array or the maximum value of several specified values passed as an argument. The function accepts an array or several numbers as an argument & return the maximum value among all the passed parameter. Its returns type is not fixed; it can be either an integer or a float value based on the given input.

Syntax

```max(array());

or

max(value1,value2,..);```
 Parameter Description Array This parameter defines an array containing values. value1,value2,… This parameter defines the two different values to compare.

Example 1

```<?php

echo (max(12, 4, 62, 97, 26));

?>```

Output

`97`

Example 2

```<?php

echo (max(array(28, 36, 87, 12)));

?>```

Output

`87`

### PHP min() Function

We can calculate the minimum value among the elements of an array by using the min function. This function helps find the minimum value in the given input array or the minimum value of several specified values passed as an argument. The function accepts an array or several numbers as an argument & return the minimum value among all the passed parameter. It returns type is not fixed; it can be either an integer value or a float value based on the given input.

Syntax

```min(array());

or

min(value1,value2,..);```
 Parameter Description Array This parameter defines an array containing values. value1,value2,… This parameter defines the two different values to compare.

Example 1

```<?php

echo (min(12, 4, 62, 97, 26));

?>```

Output

`4`

Example 2

```<?php

echo (min(array(28, 36, 87, 12)));

?>```

Output

```12
```

### PHP floor() Function

The PHP floor () function rounds a given input number down to the nearest integer. The function accepts only a single parameter, which is the number we want to round & returns the number, representing the rounded to the nearest integer.

Syntax

`floor(number);`
 Parameter Description number This parameter defines the value to round down.

Example 1

```<?php

echo floor(0.55)."<br>";

echo floor(1.45)."<br>";

echo floor(1.99);

?>```

Output

```0
1
1```

Example 2

```<?php

echo floor(8)."<br>";

echo floor(8.77)."<br>";

echo floor(-8.777)."<br>";

echo floor(34.001)."<br>";

echo floor(-0.33455);

?>```

Output

```8
8
-9
34
-1
```

### PHP random() Function

This function generates a random integer. It randomly generates a valid integer value. If we want to generate a random number between any specified value, we can pass the value as \$min & \$max parameters. The mt_rand () function also uses to generate a random value. The mt_rand() is faster than rand() function.

Syntax

`rand();`

Example

```<?php

\$randomNumber = rand();

print_r(\$randomNumber);

print_r("<br>");

\$randomNumber = rand(15,35);

print_r(\$randomNumber);

?>```

Output

```1257424548
28```

### PHP sqrt() Function

The PHP sqrt() function is useful to calculate the square root of a given number. It accepts a single parameter number whose square root is to be found. It returns the f floating-point value as output, which is the square root of a given number.

Syntax

`sqrt(number);`

Example 1

```<?php

echo(sqrt(25));

?>```

Output

`5`

Example 2

```<?php

echo(sqrt(-25));

?>```

Output

`NaN`

### Conclusion

PHP Math functions do not need to be installed as they are inbuilt. They meant only to use variables that contain numerical values, integers, and floats. I hope you have a great understanding of PHP math functions.

### Reference

Here, we have listed all the PHP math functions with a brief explanation.

 Function Description abs() This function is used to return the absolute(positive) value of a number. acos() This function gives the arc cosine of a number. acosh() This function gives the inverse hyperbolic cosine of a number. asin() This function returns the arc sine of a number asinh() This function gives the inverse hyperbolic sine of a number. atan() This function gives the arctangent of a number. atan2() This function returns the arctangent of the two variables, a and b. atanh() This function gives the inverse hyperbolic tangent of a number. base_convert() This function converts the number from one number base to another. bin_dec() It is used to Converts a binary number to a decimal number. ceil() This function rounds a number up to the nearest integer. cos() This function returns the cosine of a number. cosh() This function gives the hyperbolic cosine of a number. decbin() It is used to Convert a Decimal number to a binary number. dechex() It is used to Convert a Decimal number to a hexadecimal number. decoct() It is used to Convert a Decimal number to an octal number. deg2rad() It is used to Convert a degree value to a radian value. exp() This function calculates the exponent of e. expm1() This function returns exp(x)-1 floor() This function rounds a number down to the nearest integer. fmod() This function returns the remainder of a/b. getrandmax() This function returns the largest possible value returned by rand(). hexdec() It is used to convert a hexadecimal number to a decimal number. hypot() This function calculates the hypotenuse of a number. initdiv() This function performs integer division. is_finite() This function checks whether a value is finite or not. is_infinite() This function checks whether a value is infinite or not. is_nan() This function checks whether a value is not a number. icg_value() This function returns a pseudo-random number between 0 and 1. log() This function gives the natural logarithm of a number. log10() This function returns the base 10 logarithm of a number. log1p() This function returns the log (1+number). max() This function gives the maximum value in the array. min() This function returns the minimum value in the array. pi() This function returns the value of PI. pow() This function gives X raised to the power of y. rad2deg It is used to convert a radian value to a degree value. rand() This function generates a random integer. round() This function rounds a floating-point number. sin() This function returns the sine of a number. sinh() This function returns the hyperbolic sine of a number. sqrt() This function gives the square root of a number. tan() This function returns the tangent of a number. tanh() This function returns the hyperbolic tangent of a number.