PHP Math Functions

In this PHP tutorial, you will learn all about the Math functions used in PHP

What are Math Functions in PHP?


The math functions are the built-in PHP functions that are used for the various mathematical operations. As we all know PHP is a server-side scripting language as it needs to process various data and the PHP math function provides various support to process those mathematical operations. 


The most commonly used math function in PHP

 

PHP has various math functions for processing the data. The most used math functions are mentioned below.

  1. abs() function: PHP abs() function is used to return the absolute (positive) value of the number.
    
    abs(number);
    
    
    
    echo abs(7) . "\n";
    echo abs(-7) . "\n";
    echo abs(-3.45) . "\n";
    echo abs(9);
    
    

    Output:

    
    7
    7
    3.45
    9
    
  2. floor() function: PHP floor() function is used to return round fraction down value to the integer.
    
    floor(number);
    
    
    
    echo floor(0.45) . "\n";
    echo floor(7.29) . "\n";
    echo floor(6) . "\n";
    echo floor(8.2) . "\n";
    echo floor(-9.1) . "\n";
    echo floor(-8.9);
    
    

    Output:

    
    7
    7
    3.45
    9
    
  3. ceil() function: PHP ceil () function is used to return round fraction up value to the integer.
    
    ceil();
    
    
    
    echo ciel(-5.9);
    
    

    Output:

    
    -5
    
  4. pi() function: PHP pi() function is used to return the value of pi.
    
    pi();
    
    
    
    echo pi();
    
    

    Output:

    
    3.1415926535898
    
  5. sqrt() function: PHP sqrt() function is used to return the square root of the given value.
    
    sqrt(number);
    
    
    
    echo sqrt(4). "\n";
    echo sqrt(25). "\n";
    echo sqrt(81). "\n";
    
    

    Output:

    
    2
    5
    9
    
  6. pow() function: PHP pow() function is used to find and return the power of the given number.
    
    pow(x,y);
    
    
    
    echo pow(4, 5). "\n";
    echo pow(1.5, 3). "\n";
    echo pow(-2.5, 2);
    
    

    Output:

    
    1024
    3.375
    6.25
    
  7. decbin() function: PHP decbin() function is used to return the binary equivalent  value of the given decimal value.
    
    decbin(number);
    
    
    
    echo decbin(4). "\n";
    echo decbin(25). "\n";
    echo decbin(89);
    
    

    Output:

    
    100
    11001
    1011001
    
  8. bindec() function: PHP bindec() function is used to return the decimal equivalent  value of the given binary value.
    
    bindec(number);
    
    
    
    echo bindec(100). "\n";
    echo bindec(11001). "\n";
    echo bindec(1011001);
    
    

    Output:

    
    4
    25
    89
    
  9. dechex() function: PHP dechex() function is used to return the hexadecimal equivalent  value of the given decimal value.
    
    dechex (number);
    
    
    
    echo dechex(4). "\n";
    echo dechex(15). "\n";
    echo dechex(19);
    
    

    Output:

    
    4
    f
    13
    
  10. decoct() function: PHP decoct() function is used to return the octal equivalent  value of the given decimal value.
    
    decoct(number);
    
    
    
    echo decoct(4). "\n";
    echo decoct(15). "\n";
    echo decoct(19);
    
    

    Output:

    
    4
    17
    23
    
  11. sin() function: PHP sin() function is used to return the sine value of the give number.
    
    sin(number);
    
    
    
    echo sin(4). "\n";
    echo sin(15). "\n";
    echo sin(19);
    
    

    Output:

    
    -0.75680249530793
    0.65028784015712
    0.14987720966295
    
  12. cos() function: PHP cos() function is used to return the cosine value of the given number.
    
    cos(number);
    
    
    
    echo cos(4). "\n";
    echo cos(15). "\n";
    echo cos(19);
    
    

    Output:

    
    -0.65364362086361
    -0.75968791285882
    0.98870461818667
    
  13. tan() function: PHP tan() function is used to return the tangent value of the given number.
    
    tan(number);
    
    
    
    echo tan(4). "\n";
    echo tan(15). "\n";
    echo tan(19);
    
    

    Output:

    
    1.1578212823496
    -0.85599340090852
    0.1515894706124
    
  14. log() function: PHP log() function is used to return the natural logarithm of the given number, or to return the logarithm of the number of the given base.
    
    log(number, base(optional));
    
    
    
    echo log(4). "\n";
    echo log(0). "\n";
    echo log(5, 2);
    
    

    Output:

    
    1.3862943611199
    -INF
    2.3219280948874
    
  15. fmod() function: PHP fmod() function is used to return the reminder of the given number.
    
    fmod(x,y);
    
    
    
    echo fmod(18, 3). "\n";
    echo fmod(12, 5). "\n";
    echo fmod(5, 2);
    
    

    Output:

    
    0
    2
    1