Math Functions in PHP


Math functions allow you to perform various operations on numeric values.
Math functions are explained in detail below:

(1) abs:

abs () function accepts number as an argument and returns the absolute value of a number specified as an argument.
Syntax:
abs (Number)

<?php
echo abs(-1.5)."<br/>";
echo abs(1.5)."<br/>";
?>
Output:
1.5
1.5


(2) ceil:

ceil () function accepts a Number as an argument and returns the number which is rounded upwards to the nearest integer value.
Syntax:
ceil (Number)

<?php
echo ceil(1.4)."<br/>";
echo ceil(-1.4)."<br/>";
?>
Output:
2
-1

     

(3) floor:

floor () function accepts a Number as an argument and returns the number which is rounded downwards to the nearest integer value.
Syntax:
floor (Number)

<?php
echo floor (1.4)."<br/>";
echo floor (-1.4)."<br/>";
?>
Output:
1
-2


(4) round:

round () function accepts a Number as an argument and returns a number rounded to the nearest integer.
You can also specify the precision of the digits that is displayed after the decimal point. However it is optional argument if it is not specified then it will not display any digits after decimal point.
Syntax:
round (Number [, Precision])

<? php
echo round (1.75)."<br/>";
echo round (1.23)."<br/>";
?>
Output:
2
1


(5) fmod:

fmod () function accepts two numbers Number1 and Number2 as an argument.
It divides Number1 by Number2 and returns the remainder of the division.
Syntax:
fmod (Number1, Number2)

<?php
$a=5;
$b=2;
$c=fmod($a,$b);
echo $c;
?>
Output:
1


(6) min:

min () function accepts two Numbers Number1 and Number2 as argument and returns the number having lowest value among them.
Syntax:
min (Number1, Number2)

<?php
$a=5;
$b=2;
$c=min($a,$b);
echo $c;
?>
Output:
2


(7) max:

max () function accepts two Numbers Number1 and Number2 as argument and returns the number having highest value among them.
Syntax:
max (Number1, Number2)

<?php
$a=5;
$b=2;
$c=max ($a, $b);
echo $c;
?>
Output:
5


(8) pow:

pow () function accepts two numbers Number1 and Number2 as an argument and raises the Number1 to the power of the Number2, and returns the result.
Syntax:
pow (Number1, Number2)

<?php
echo pow(2,3)."<br/>";
echo pow(-2,3)."<br/>";
echo pow(3,-2)."<br/>";
?>
Output:
8
-8
0.11111111111111


(9) sqrt:

sqrt () function accepts a number as an argument and returns the square root of a number.
If the number specified as an argument is negative then it returns NAN.
Syntax:
sqrt (Number)

<?php
echo sqrt(49)."<br/>";
echo sqrt(-49)."<br/>";
echo sqrt(49.49)."<br/>";
?>
Output:
7
NAN
7.0349129347846


(10) rand:

rand () function is used to generates a random integer between the range 0 to RAND_MAX. The value of RAND_MAX is 32768.
It also takes two optional arguments which specifies the range for the rand () function.
Syntax:
rand ([min] [, max])
Here min and max specifies the range between which the random number is generated each time the function is called.

<?php
$c=rand ();
echo $c;
?>
Output:
It will generate random number between the range 0 to RAND_MAX.
Example2:
<? php
$c=rand (1, 100);
echo $c;
?>
Output:
It will generate random number between the range 1 to 100.

Download Projects


Download Programs