PHP Math


PHP offers a collection of math features to execute number-related math operations.

You can also search for these topics, math function using php, math function examples, how to use math function in php, define math function.

PHP pi() Function

The PI value is returned by the pi() function.

Example :-

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

Output :-

3.1415926535898

You can also search for these topics, php pi() function, php pi() function example, php pi value, php pi value returns.

PHP min() and max() Functions

In a list of inputs, the min() and max() methods can be used to get the lowest and highest number.

Example :-

<?php
echo(min(0, 150, 30, 20, -8, -200));  // returns -200
echo(max(0, 150, 30, 20, -8, -200));  // returns 150
?>

Output :-

-200
150

You can also search for these topics, php min() and max() function, php min() and max() function example, php min() function, php max() function, find the maximum value using php, find the minimum value using php.

PHP abs() Function

The absolute (positive) value of an integer is returned by the abs() function.

Example :-

<?php
echo(abs(-6.7));  // returns 6.7
echo "<br />";
echo(abs(-3.1));  // returns 3.1
echo "<br />";
echo(abs(2));  // returns 2
?>

Output :-

6.7
3.1
2

You can also search for these topics, php abs() function, php abs() example, php abs() value, php abs() positive, php abs() negative, php abs() method.

PHP sqrt() Function

The square root of an integer is returned by the sqrt() function.

Example :-

<?php
echo(sqrt(64));  // returns 8
echo "<br />";
echo(sqrt(9));  // returns 3
?>

Output :-

8
3

You can also search for these topics, php sqrt() function, php sqrt() function example, sqrt() php variable, php echo sqrt().

PHP round() Function

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

Example :-

<?php
echo(round(0.60));  // returns 1
echo(round(0.49));  // returns 0
?>

Output :-

1
0

You can also search for these topics, php round() function, php round() function example, rounding the number using php, php echo round.

Random Numbers

The rand() function is used to generate or create an random integer number.

Example 1 :-

<?php
echo(rand());
echo "<br />";
echo(rand());
?>

Output :-

1381167960
1000111223

Note :- It will generate any number between minimum and maximum of an integer value.

Example 2 :- If you want a random integer between 10 and 100 (inclusive), use rand(10, 100):

<?php
echo(rand(10, 100));
echo "<br />";
echo(rand(10, 100));
?>

Output :-

97
32

You can also search for these topics, php random numbers, php rand() function, php rand() function example, use of random function, how to generate random numbers using php.

Complete PHP Math Reference

For a complete reference of all math functions, go to our complete PHP Math Reference.

The PHP math reference contains description and example of use, for each function.