In this tutorial we will learn some of the basic math functions used in PHP.
This function returns the absolute (positive) value of the given number.
Syntax
abs(number)
For Ex.
<?php echo abs(7.8)."<br>"; echo abs(-7.8)."<br>"; echo abs(-7)."<br>"; echo abs(7)."<br>"; ?>
Output
7.8 7.8 7 7
This function rounds up the given number to the nearest integer number.
Syntax
ceil(number)
This function rounds down the given number to the nearest integer number.
Syntax
floor(number)
This function rounds a given floating point number.
Syntax
round(number)
For Ex.
<?php echo ceil(7.8)."<br>"; echo floor(7.8)."<br>"; echo round(7.3)."<br>"; echo round(7.6)."<br>"; ?>
Output
8 7 7 8
As you can see from the above example ceil() function rounds up the floating point number 7.8 to 8 same way floor() function rounds down the floating point number 7.8 to 7 and round() function rounds the floating point number on basis of the entered value if the value after point is >= .5 than it rounds up the value otherwise it rounds down the value to nearest integer.
This function returns x raised to the power of y.
Syntax
pow(x,y)
For Ex.
<?php echo pow(2,3)."<br>"; echo pow(3,2)."<br>"; ?>
Output
8 9
This function returns the square root of the entered number.
Syntax
sqrt(number)
For Ex.
<?php echo sqrt(4)."<br>"; echo sqrt(9)."<br>"; ?>
Output
2 3
This function returns e raised to the power of given number.
Syntax
exp(number)
For Ex.
<?php echo exp(1)."<br>"; echo exp(2)."<br>"; ?>
Output
2.718281828459 7.3890560989307
This function converts the entered decimal number into binary number.
Syntax
decbin(number)
This function converts the entered decimal number into hexadecimal number.
Syntax
dechex(number)
This function converts the entered decimal number into octal number.
Syntax
decoct(number)
For Ex.
<?php echo "Hexadecimal value of 10 = ".dechex(10)."<br>"; echo "Octal value of 10 = ".decoct(10)."<br>"; echo "Binary value of 10 = ".decbin(10)."<br>"; ?>
Output
Hexadecimal value of 10 = a Octal value of 10 = 12 Binary value of 10 = 1010
This function converts the entered binary number into decimal number.
Syntax
bindec(number)
This function converts the entered octal number into decimal number.
Syntax
octdec(number)
This function converts the entered hexadecimal number into decimal number.
Syntax
hexdec(number)
For Ex.
<?php echo hexdec('a')."<br>"; echo octdec(12)."<br>"; echo bindec(1010)."<br>"; ?>
Output
10 10 10
This function returns the remainder of modulo division of x with y.
Syntax
fmod(x,y)
For Ex.
<?php echo fmod(10,3)."<br>"; echo fmod(13,5)."<br>"; ?>
Output
1 3
This function returns the highest value among the given set of values or the highest value in array.
Syntax.
max(numeric_array) or max(numeric_values)
For Ex.
<?php $a=array(23,21,12,34,13); echo "Maximum Value in array = ".max($a)."<br>"; ?>
Output
Maximum Value in array = 34
This function returns the minimum value among the given set of values or the minimum value in array.
Syntax.
min(numeric_array) or min(numeric_values)
For Ex.
<?php $a=array(23,21,12,34,13); echo "Minimum Value in array = ".min($a)."<br>"; ?>
Output
Minimum Value in array = 12
Note that max() and min() can also be used with set of numeric values as argument in place of array.
This functions returns the sine, cosine and tangent of a given number.
Syntax
sin(number) cos(number) tan(number)
For Ex.
<?php echo "sin(45) = ".sin(45)."<br>"; echo "cos(45) = ".cos(45)."<br>"; echo "tan(45) = ".tan(45)."<br>"; ?>
Output
sin(45) = 0.85090352453412 cos(45) = 0.52532198881773 tan(45) = 1.6197751905439
To conclude in this tutorial we have learnt math functions used in PHP which will be very useful for mathematical calculations.