Pin Me

Using Math Functions in PHP

written by: •edited by: Michele McDonough•updated: 7/16/2010

PHP includes a lot of built in math functions which you can use in your program. In this tutorial, I will list the most commonly used ones along with their syntax, examples, and descriptions.

  • slide 1 of 1

    Mathematical Functions in PHP

    PHP is a very powerful scripting language. It has a lot of built in functions which help you do stuff much faster than it would take if you coded those functions yourself. There are some really good functions in PHP which you can use for mathematical operations.

    In this tutorial, I will introduce some really helpful math functions and explain how you can use them in your PHP program.

    Power Function - pow()

    Syntax: pow(a,b)

    This will return the value a ^ b.

    Example: pow(2,4) returns 16

    Pi Function - pi()

    Syntax: pi()

    This will return the value of the mathematical constant Pi: 3.141592.

    Random Function - rand()

    Syntax: rand()

    This will return any random integer.

    Example: rand() may return 1254

    Optional Syntax: rand(minimum, maximum)

    This will return a random number in the range minimum to maximum.

    Example: rand (10,20) may return any number between 10 and 20.

    Maximum Function - max()

    Syntax: max(a,b)

    This will return the maximum value in a or b.

    Example: max(5,10) will return 10

    Minimum Function - min()

    Syntax: min(a,b)

    This will return the minimum value in a or b.

    Example: min(5,10) will return 5

    Square Root Function - sqrt()

    Syntax: sqrt(a)

    This will return the square root of a.

    Example: sqrt(9) will return 3

    Logarithm Function - log()

    Syntax: log(a,x)

    This will return the value of log a to the base x. If no base is specified, the base is assumed to be "e".


    log(2) returns 0.6931

    log(2,2) returns 1

    log(2.718) returns 1.000000xxx (a the value of E is 2.718)

    Exponent Function - exp()

    Syntax: exp(a)

    This will return the value of e raised to the ath power.

    Example: exp(2) will return 2.718 ^ 2 = 7.3890561

    Absolute Value function - abs()

    Syntax: abs(a)

    This will return the absolute value of a.

    Example: abs(-5) returns 5, abs(10) returns 10

    Rounding Function - round()

    Syntax: round(a)

    This will round the number a to its nearest integer value.


    round(1.4) returns 1

    round(1.6) returns 2

    Ceiling Function - ceil()

    Syntax: ceil(a)

    This will return the smallest integer greater than the value a.


    ceil(1.4) returns 2

    ceil(-1.4) returns -1

    Floor Function - floor()

    Syntax: floor(a)

    This will return the biggest integer smaller than the value a.


    ceil(1.4) returns 1

    ceil(-1.4) returns -2

    Sine Function - sin()

    Syntax: sin(a)

    This will return the sine value of a.

    Example: sin(2) returns 0.9092

    Cosine Function - cos()

    Syntax: cos(a)

    This will return the cosine value of a.

    Example: cos(2) returns -0.4161

    Tangent Function - tan()

    Syntax: tan(a)

    This will return the tangent value of a.

    Example: tan(2) returns -2.185

    Binary to Decimal Function - bindec()

    Syntax: bindec(binary_a)

    This returns the decimal value of the binary number a.

    Example: bindec("111") returns 7

    Hexadecimal to Decimal Function - hexdec()

    Syntax: hexdec(hex_a)

    This returns the decimal value of the hexadecimal number a.

    Example: hexdec("11") returns 17

    Similarly there are also functions to convert decimal to hexadecimal (dechex) and binary (decbin).

    These are the math functions that you will use frequently when creating scripts in PHP. Whenever you have any doubts, just check out any PHP reference book for the exact parameters to be entered in any function.

More To Explore