PHP Math Functions

Manas Singh 28th Aug 2016

In this tutorial we will learn some of the basic math functions used in PHP.

abs()

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

ceil()

This function rounds up the given number to the nearest integer number.

Syntax

ceil(number)

floor()

This function rounds down the given number to the nearest integer number.

Syntax

floor(number)

round()

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.

pow()

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

sqrt()

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

exp()

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

decbin()

This function converts the entered decimal number into binary number.

Syntax

decbin(number)

dechex()

This function converts the entered decimal number into hexadecimal number.

Syntax

dechex(number)

decoct()

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

bindec()

This function converts the entered binary number into decimal number.

Syntax

bindec(number)

octdec()

This function converts the entered octal number into decimal number.

Syntax

octdec(number)

hexdec()

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

fmod()

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

max()

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

min()

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.

sin() , cos() , tan()

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.

Authored By Manas Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk