Math Functions in PHP

phpApart from arithmetic operators, we also have Math functions in PHP.  Click Here for list of Math functions in PHP.  There are few commonly used Math functions given below.

Is_int:  This function will check if the passed value is integer or not and return true or false.

Is_float:  This function will check if the passed value is float or not and return true or false.

Is_numeric:  This function will check if the passed value is numeric or not and return true or false.  Both float and integer are considered as numeric.

Rand:  This function will return random value. We do have overloaded version of this function which will take min and max value as parameter and return the random value within that range.

Round:  This function will take 2 parameters, a float and an integer which will determine how much digits you want after the decimal.

Pow:  This is also known as exponential expression. It will take 2 integers as parameter, base and exponent.  Base will act as power to the exponent.

Sqrt:  This function will return square root value.

Fmod:  This function is similar to arithmetic operator modulus (%). This function will return remainder of the division.

Abs:  This function will return the absolute value of an integer or float.

Ceil:  This function will round up the float number.

Floor:  This function will round down the float number.

<?php

$n1 = 49;
$n2 = 3.14;

echo 'Is int: '. is_int($n1) .'<br />';
echo 'Is float: '. is_float($n2) .'<br />';
echo 'Is numeric: '. is_numeric($n1) .'<br />';
echo 'Rand: '. Rand() .'<br />';
echo 'Rand(min, max): '. Rand(1,20) .'<br />';
echo 'Round: '. round($n2, 1) .'<br />';
echo 'Pow: '. pow($n1, 7) .'<br />';
echo 'Squrt: '. sqrt($n1) .'<br />';
echo 'Fmod: '. fmod($n1, 5) .'<br />';
echo 'Abs: '. abs(-582) .'<br />';
echo 'Ceil: '. ceil($n2) .'<br />';
echo 'Floor: '. floor($n2) .'<br />';
 
?>