PHP 数学函数


2021年12月16日, Learn eTutorial
2264

在本 PHP 教程中,您将学习 PHP 中使用的所有数学函数。

PHP 中的数学函数是什么?


数学函数是 PHP 的内置函数,用于各种数学运算。我们都知道 PHP 是一种服务器端脚本语言,因为它需要处理各种数据,而 PHP 数学函数为处理这些数学运算提供了各种支持。


PHP 中最常用的数学函数

 

PHP 有多种用于处理数据的数学函数。下面提到了最常用的数学函数。

  1. abs() 函数:PHP abs() 函数用于返回数字的绝对值(正值)。
    
    abs(number);
    
    
    
    echo abs(7) . "\n";
    echo abs(-7) . "\n";
    echo abs(-3.45) . "\n";
    echo abs(9);
    
    

    输出

    
    7
    7
    3.45
    9
    
  2. floor() 函数:PHP floor() 函数用于将小数值向下舍入到整数。
    
    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);
    
    

    输出

    
    7
    7
    3.45
    9
    
  3. ceil() 函数:PHP ceil() 函数用于将小数值向上舍入到整数。
    
    ceil();
    
    
    
    echo ciel(-5.9);
    
    

    输出

    
    -5
    
  4. pi() 函数:PHP pi() 函数用于返回 pi 的值。
    
    pi();
    
    
    
    echo pi();
    
    

    输出

    
    3.1415926535898
    
  5. sqrt() 函数:PHP sqrt() 函数用于返回给定值的平方根。
    
    sqrt(number);
    
    
    
    echo sqrt(4). "\n";
    echo sqrt(25). "\n";
    echo sqrt(81). "\n";
    
    

    输出

    
    2
    5
    9
    
  6. pow() 函数:PHP pow() 函数用于查找并返回给定数字的幂。
    
    pow(x,y);
    
    
    
    echo pow(4, 5). "\n";
    echo pow(1.5, 3). "\n";
    echo pow(-2.5, 2);
    
    

    输出

    
    1024
    3.375
    6.25
    
  7. decbin() 函数:PHP decbin() 函数用于返回给定十进制值等效的二进制值。
    
    decbin(number);
    
    
    
    echo decbin(4). "\n";
    echo decbin(25). "\n";
    echo decbin(89);
    
    

    输出

    
    100
    11001
    1011001
    
  8. bindec() 函数:PHP bindec() 函数用于返回给定二进制值等效的十进制值。
    
    bindec(number);
    
    
    
    echo bindec(100). "\n";
    echo bindec(11001). "\n";
    echo bindec(1011001);
    
    

    输出

    
    4
    25
    89
    
  9. dechex() 函数:PHP dechex() 函数用于返回给定十进制值等效的十六进制值。
    
    dechex (number);
    
    
    
    echo dechex(4). "\n";
    echo dechex(15). "\n";
    echo dechex(19);
    
    

    输出

    
    4
    f
    13
    
  10. decoct() 函数:PHP decoct() 函数用于返回给定十进制值等效的八进制值。
    
    decoct(number);
    
    
    
    echo decoct(4). "\n";
    echo decoct(15). "\n";
    echo decoct(19);
    
    

    输出

    
    4
    17
    23
    
  11. sin() 函数:PHP sin() 函数用于返回给定数字的正弦值。
    
    sin(number);
    
    
    
    echo sin(4). "\n";
    echo sin(15). "\n";
    echo sin(19);
    
    

    输出

    
    -0.75680249530793
    0.65028784015712
    0.14987720966295
    
  12. cos() 函数:PHP cos() 函数用于返回给定数字的余弦值。
    
    cos(number);
    
    
    
    echo cos(4). "\n";
    echo cos(15). "\n";
    echo cos(19);
    
    

    输出

    
    -0.65364362086361
    -0.75968791285882
    0.98870461818667
    
  13. tan() 函数:PHP tan() 函数用于返回给定数字的正切值。
    
    tan(number);
    
    
    
    echo tan(4). "\n";
    echo tan(15). "\n";
    echo tan(19);
    
    

    输出

    
    1.1578212823496
    -0.85599340090852
    0.1515894706124
    
  14. log() 函数:PHP log() 函数用于返回给定数字的自然对数,或返回给定底数的数字的对数。
    
    log(number, base(optional));
    
    
    
    echo log(4). "\n";
    echo log(0). "\n";
    echo log(5, 2);
    
    

    输出

    
    1.3862943611199
    -INF
    2.3219280948874
    
  15. fmod() 函数:PHP fmod() 函数用于返回给定数字的余数。
    
    fmod(x,y);
    
    
    
    echo fmod(18, 3). "\n";
    echo fmod(12, 5). "\n";
    echo fmod(5, 2);
    
    

    输出

    
    0
    2
    1