PHP Math Functions with Examples - PHP Math Operations



  • PHP has a set of math functions that allows you to perform mathematical tasks on numbers.
php-math

PHP pi() Function

  • The pi() function returns the value of PI:

Sample Code

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

Output

3.1415926535898

PHP min() and max() Functions

  • The min() and max() functions can be used to find the lowest or highest value in a list of parameters:

Sample Code

<?php
echo(min(0, 250, 49, 110, -88, -200)); 
echo(max(0, 300, 49, 110, -88, -200));
?>

Output

-200
300

PHP abs() Function

  • The abs() function returns the absolute (positive) value of a number

Sample Code

<?php
echo(abs(-11.25));
?>

Output

11.25

PHP sqrt() Function

  • The sqrt() function returns the square root of a number:

Sample Code

<?php
echo(sqrt(36));
?>

Output

6

PHP round() Function

  • The round() function rounds a floating-point number to its nearest integer:

Sample Code

<?php
echo(round(0.74));
echo(round(0.44));
?>

Output

1
0

Random Numbers

  • The rand() function generates a random number:

Sample Code

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

Output

944034960

  • To control the random number values you have to give min and max values to show in the output

Sample Code

<?php
echo(rand(150,300));
?>

Output

126


Related Searches to PHP Math Functions with Examples - PHP Math Operations