PHP Math Functions

1. abs() - Absolute value

<?php 
 echo abs("-5.4");  // Output 5.2
 echo abs("-4");  // Output 4
?>

2. ceil() - Round fractions up

<?php 
 echo ceil("4.4");  // Output 5
 echo ceil("8.99");  // Output 9
 echo ceil("-3.15");  // Output -3
?>

3. floor() - Round fractions down

<?php 
 echo ceil("4.4");  // Output 4
 echo ceil("8.99");  // Output 8
 echo ceil("-3.15");  // Output -4
?>

4. max() - Find highest value

<?php 
 echo max("1, 5, 7, 9, 12");  // Output 12
 echo max(array(1, 5, 7, 9));  // Output 9
?>

5. min() - Find lowest value

<?php 
 echo min("2, 5, 7, 9, 12");  // Output 2
 echo min(array(3, 5, 7, 9));  // Output 3
?>

6. pi() - Get value of pi

<?php 
 echo pi();  // Output 3.1415926535898
 echo M_PI;  // Output 3.1415926535898
?>

7. pow() - Exponential expression

<?php 
 echo pow(2, 8);// Output 256
?>

8. round() - Rounds a float

float round($val, $precision, $mode = PHP_ROUND_HALF_UP)

Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

Parametes:

val: The value to round

precision: The optional number of decimal digits to round to.

mode: Use one of the following constants to specify the mode in which rounding occurs

<?php 
 echo round(5.4);  // Output 5
 echo round(5.5);  // Output 6
 echo round(2.98563, 2);  // Output 2.99
 echo round(4.05685, 2);  // Output 4.06
?>