Php 简明教程

PHP – Maths Functions

为启用数学运算,PHP 具有数学(算术)运算符和许多数学函数。在本章中,将通过示例说明以下数学函数。

To enable mathematical operations, PHP has mathematical (arithmetic) operators and a number of mathematical functions. In this chapter, the following mathematical functions are explained with examples.

PHP abs() Function

abs() 函数是 PHP 解释器的内置函数。此函数接受任意数字作为参数并返回一个正值,而不考虑其符号。任何数字的绝对值始终为正。

The abs() function is an in-built function in PHP iterpreter. This function accepts any number as argument and returns a positive value, disregarding its sign. Absolute value of any number is always positive.

abs( mixed $num)

PHP abs() 函数返回 num 的绝对值。如果 num 的数据类型是浮点数,它的返回类型也将是浮点数。对于整数参数,返回类型为整数。

PHP abs() function returns the absolute value of num. If the data type of num is float, its return type will also be float. For integer parameter, the return type is integer.

Example

请查看以下示例:

Take a look at this following example −

<?php
   $num=-9.99;
   echo "negative float number: " . $num . "\n";
   echo "absolute value : " . abs($num) . "\n";

   $num=25.55;
   echo "positive float number: " . $num . "\n";
   echo "absolute value : " . abs($num). "\n";

   $num=-45;
   echo "negative integer number: " . $num . "\n";
   echo "absolute value : " . abs($num) . "\n";

   $num=25;
   echo "positive integer number: " . $num . "\n";
   echo "absolute value : " . abs($num);
?>

它将生成以下 output

It will produce the following output

negative float number: -9.99
absolute value : 9.99
positive float number: 25.55
absolute value : 25.55
negative integer number: -45
absolute value : 45
positive integer number: 25
absolute value : 25

PHP ceil() Function

ceil() 函数是 PHP 解释器的内置函数。此函数接受任意浮点数作为参数并将其四舍五入到下一个最高整数。此函数始终返回一个浮点数,因为浮点数的范围大于整数范围。

The ceil() function is an in-built function in PHP iterpreter. This function accepts any float number as argument and rounds it up to the next highest integer. This function always returns a float number as the range of float is bigger than that of integer.

ceil ( float $num ) : float

PHP ceil() 函数返回大于或等于给定参数的最小整数值。

PHP ceil() function returns the smallest integer value that is bigger than or equal to given parameter.

Example 1

以下代码将 5.78 四舍五入到其下一个最高整数 6

The following code rounds 5.78 to its next highest integer which is 6

<?php
   $arg=5.78;
   $val=ceil($arg);
   echo "ceil(" . $arg .  ") = " . $val;
?>

它将生成以下 output

It will produce the following output

ceil(5.78) = 6

Example 2

以下示例演示了如何查找 15.05 的下一个最高整数。

The following example shows how you can find the next highest integer of 15.05.

<?php
   $arg=15.05;
   $val=ceil($arg);
   echo "ceil(" . $arg .  ") = " . $val;
?>

它将生成以下 output

It will produce the following output

ceil(15.05) = 16

Example 3

对于负数,它会四舍五入到 0。

For negative number, it is rounded towards 0.

<?php
   $arg=-3.95;
   $val=ceil($arg);
   echo "ceil(" . $arg .  ") = " . $val;
?>

它将生成以下 output

It will produce the following output

ceil(-3.95) = -3

PHP exp() Function

exp() 函数计算欧拉数 e 的指数。PHP 有一个预定义常量 M_E,表示欧拉数,等于 2.7182818284590452354。因此,exp(x) 返回 2.7182818284590452354x

The exp() function calculates exponent of e that is Euler Number. PHP has a predefined constant M_E that represents Euler Number and is equal to 2.7182818284590452354. Hence, exp(x) returns 2.7182818284590452354x

此函数始终返回一个浮点数。

This function always returns a float.

exp ( float $arg ) : float

PHP exp() 函数返回提升到给定 arg. 的欧拉数 e。请注意, e 是自然算法的底数。exp() 函数是自然对数的逆函数。

PHP exp() function returns the Euler Number e raised to given arg. Note that e is the base of natural algorithm. The exp() function is the inverse of natural logarithm.

Example 1

PHP 中的一个预定义常量是 M_LN2 ,它表示 loge2,等于 0.69314718055994530942。因此,此值的 exp() 将返回 2。

One of the predefined constants in PHP is M_LN2 which stands for loge2 and is equal to 0.69314718055994530942. So, the exp() of this value will return 2.

<?php
   echo "exp(" . M_LN2 . ") = " . exp(M_LN2);
?>

它将生成以下 output

It will produce the following output

exp(0.69314718055995) = 2

Example 2

M_LN10 是表示 loge10 的另一个预定义常量。此程序计算 exp(M_LN10) 并返回 10。

M_LN10 is another predefined constant representing loge10. This program calculates exp(M_LN10) and returns 10.

<?php
   echo "exp(" . M_LN10 . ") = " . exp(M_LN10);
?>

它将生成以下 output

It will produce the following output

exp(2.302585092994) = 10

PHP floor() Function

floor() 函数是 PHP 解释器中的另一个内置函数。此函数接受任意浮点数字作为参数,并根据其向下取整到最接近的整数。此函数始终返回浮点数,因为浮点值的范围大于整数值的范围。

The floor() function is another in-built function in PHP interpreter. This function accepts any float number as argument and rounds it down to the next lowest integer. This function always returns a float number as the range of float is bigger than that of integer.

floor ( float $num ) : float

PHP floor() 函数返回不小于给定参数的最大整数。

PHP floor() function returns the largest integer less than or equal to the given parameter.

Example 1

以下示例展示了如何将 15.05 舍入至下一个最大整数,即 15

The following example shows how to round 15.05 to its next highest integer which is 15

<?php
   $arg=15.05;
   $val=floor($arg);
   echo "floor(" . $arg .  ") = " . $val;
?>

它将生成以下 output

It will produce the following output

floor(15.05) = 15

Example 2

以下示例展示了如何找出 5.78 的下一个最小整数。

The following example shows how to find the next lowest integer of 5.78.

<?php
   $arg=5.78;
   $val=floor($arg);
   echo "floor(" . $arg .  ") = " . $val;
?>

它将生成以下 output

It will produce the following output

floor(5.78) = 5

Example 3

负数从 0 中舍去。

Negative numbers are rounded away from 0.

<?php
   $arg=-3.95;
   $val=floor($arg);
   echo "floor(" . $arg .  ") = " . $val;
?>

它将生成以下 output

It will produce the following output

floor(-3.95) = -4

PHP intdiv() Function

intdiv() 函数返回两个整型参数的整数商。如果 x/y 除法结果为“i”,余数为“r”,则为 −

The intdiv() function returns the integer quotient of two integer parameters. If x/y results in "i" as division and "r" as remainder, then −

x = y*i+r

在这种情况下,intdiv(x,y) 返回“i”

In this case, intdiv(x,y) returns "i"

intdiv ( int $x , int $y ) : int

“x”参数是除法表达式的分子部分,而“y”参数是除法表达式的分母部分。

The "x" parameter forms numerator part of the division expression, while the "y" parameter forms the denominator part of the division expression.

PHP intdiv() 函数返回“x”除以“y”的整数商。如果两个参数都是正数或都是负数,则返回值为正数。

PHP intdiv() function returns the integer quotient of division of "x" by "y". The return value is positive if both the parameters are positive or both the parameters are negative.

Example 1

以下示例表明,如果分子小于分母,则 intdiv() 函数返回 0。

The following example shows that if the numerator is less than the denominator, then intdiv() function returns 0.

<?php
   $x=10;
   $y=3;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
   $r=intdiv($y, $x);
   echo "intdiv(" . $y . "," . $x . ") = " . $r;
?>

它将生成以下 output

It will produce the following output

intdiv(10,3) = 3
intdiv(3,10) = 0

Example 2

在以下示例中,intdiv() 函数返回负数,因为分子或分母是负数。

In the following example, intdiv() function returns a negative integer because either the numerator or the denominator is negative.

<?php
   $x=10;
   $y=3;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";

   $x=10;
   $y=-3;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";

   $x=-10;
   $y=3;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";

   $x=-10;
   $y=-3;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r ;
?>

它将生成以下 output

It will produce the following output

intdiv(10,3) = 3
intdiv(10,-3) = -3
intdiv(-10,3) = -3
intdiv(-10,-3) = 3

Example 3

在以下示例中,分母为 0。它导致 DivisionByZeroError 异常。

Denominator is 0 in the following example. It results in DivisionByZeroError exception.

<?php
   $x=10;
   $y=0;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
?>

它将生成以下 output

It will produce the following output

PHP Fatal error:  Uncaught DivisionByZeroError: Division by zero

Example 4

两个参数中的小数部分都会被忽略。PHP intdiv() 函数仅适用于整数部分。

The fractional parts in both the parameters are ignored. PHP intdiv() function is applied only to the integer parts.

<?php
   $x=2.90;
   $y=1.90;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "";
?>

它将生成以下 output

It will produce the following output

intdiv(2.9,1.9) = 2

PHP log10() Function

log10 () 函数计算某个数字的以 10 为底的对数。以 10 为底的对数也称为 commonstandard algorithm 。log10(x) 函数计算 log10x。它与自然算法之间的关系由以下等式表示 −

The log10 () function calculates the base-10 logarithm of a number. Base-10 logarithm is also called common or standard algorithm. The log10(x) function calculates log10x. It is related to natural algorithm by the following equation −

log10x=logex/loge10 ; So that
log10100=loge100/loge10 = 2

在 PHP 中, log10log10() 函数表示

In PHP, log10 is represented by log10() function

log10 ( float $arg ) : float

PHP log10() 函数返回 arg 的以 10 为底的对数。

PHP log10() function returns the base-10 logarithm of arg.

Example 1

以下代码计算 100 的以 10 为底的对数

The following code calculates the base-10 logarithm of 100

<?php
   $arg=100;
   echo "log10(" . $arg. ")=" . log10($arg) . "";
?>

它将生成以下 output

It will produce the following output

log10(100)=2

Example 2

以下代码计算欧拉数 M_E 的以 10 为底的对数。结果等于预定义的常数 M_LOG10E

The following code calculates the base-10 logarithm of Euler Number M_E. The result is equal to a predefined constant M_LOG10E

<?php
   $arg=M_E;
   echo "log10(" . $arg. ")=" . log10($arg) . "\n";
   echo "predefined constant M_LOG10E=" . M_LOG10E;
?>

它将生成以下 output

It will produce the following output

log10(2.718281828459)=0.43429448190325
predefined constant M_LOG10E=0.43429448190325

Example 3

以下代码计算 log100 并返回 -∞。

The following code calculates log100 and returns -∞.

<?php
   $arg=0;
   echo "log10(" . $arg. ")=" . log10($arg) . "";
?>

它将生成以下 output

It will produce the following output

log10(0)=-INF

Example 4

平方根(-1)的结果也是 NaN。所以它的 log10() 也返回 NaN。

Similarly sqrt(-1) results in NAN. Hence, its log10() also returns NAN.

<?php
   $arg=sqrt(-1);
   echo "log10(" . $arg. ")=" . log10($arg) . "";
?>

它将生成以下 output

It will produce the following output

log10(NAN)=NAN

PHP max() Function

max() 函数返回数组中最大的元素,或者返回两个或两个以上逗号分隔参数中最大的参数。

The max () function returns the highest element in an array, or the highest amongst two or more comma separated parameters.

max ( array $values ) : mixed

或者,

Or,

max ( mixed $value1 [, mixed $... ] ) : mixed
  1. If only one parameter is given, it should be an array of values which may be of same or different types.

  2. If two or more parameters are given, they should be any comparable values of same or different types.

PHP max() 函数返回数组参数或值序列中的最大值。标准比较运算符适用。如果不同类型的值有多个被评估为相等(例如 0 和’PHP'),则将返回函数的第一个参数。

PHP max() function returns the highest value from the array parameter or sequence of values. Standard comparison operators are applicable. If multiple values of different types evaluate as equal (e.g. 0 and 'PHP'), the first parameter to the function will be returned.

Example 1

下面的代码返回数字数组中的最大值。

The following code returns the highest value from a numeric array.

<?php
   $arg=array(23, 5.55, 142, 56, 99);
   echo "array=";
   foreach ($arg as $i) echo $i . ",";
   echo "\n";
   echo "max = " . max($arg);
?>

它将生成以下 output

It will produce the following output

array=23,5.55,142,56,99,
max = 142

Example 2

下面的代码返回字符串数组中的 max()。

The following code returns max() from an array of strings.

<?php
   $arg=array("Java", "Angular", "PHP", "C", "Kotlin");
   echo "array=";
   foreach ($arg as $i) echo $i . ",";
   echo "\n";
   echo "max = " . max($arg);
?>

它将生成以下 output

It will produce the following output

array=Java,Angular,PHP,C,Kotlin,
max = PHP

Example 3

在下面的示例中,向 max() 函数提供了一系列字符串值。让我们看看它如何表现 −

In the following example, a series of string values is provided to the max() function. Let’s see how it behaves −

<?php
   $val1="Java";
   $val2="Angular";
   $val3="PHP";
   $val4="C";
   $val5="Kotlin";
   echo "values=" . $val1 . "," . $val2 . "," . $val3 . "," . 	$val4 . "," . $val5 . "\n";
   echo "max = " . max($val1, $val2, $val3,$val4,$val5);
?>

它将生成以下 output

It will produce the following output

values=Java,Angular,PHP,C,Kotlin
max = PHP

Example 4

在此示例中,给定的数组是混合数据类型的集合。

In this example, the given array is a collection of mixed data types.

<?php
   $arg=array(23, "Java", 142, 1e2, 99);
   echo "array=";
   foreach ($arg as $i) echo $i . ",";
   echo "\n";
   echo "max = " . max($arg);
?>

它将生成以下 output

It will produce the following output

array=23,Java,142,100,99,
max = 142

PHP min() Function

min() 函数返回数组中最小的元素,或者返回两个或两个以上逗号分隔参数中最小的参数。

The min () function returns the lowest element in an array, or the lowest amongst two or more comma separated parameters.

min ( array $values ) : mixed

或者,

Or,

min ( mixed $value1 [, mixed $... ] ) : mixed
  1. If only one parameter is given, it should be an array of values which may be of same or different types

  2. If two or more parameters are given, they should be any comparable values of same or different types

PHP min() 函数返回数组参数或值序列中的最小值。标准比较运算符适用。如果不同类型的值有多个被评估为相等(例如 0 和’PHP'),则将返回函数的第一个参数

PHP min() function returns the lowest value from the array parameter or sequence of values. Standard comparison operators are applicable. If multiple values of different types evaluate as equal (e.g. 0 and 'PHP'), the first parameter to the function will be returned

Example 1

下面的代码返回数字数组中的最小值。

The following code returns the smallest value from numeric array.

<?php
   $arg=array(23, 5.55, 142, 56, 99);
   echo "array=";
   foreach ($arg as $i) echo $i . ",";
   echo "\n";
   echo "min = " . min($arg);
?>

它将生成以下 output

It will produce the following output

array=23,5.55,142,56,99,
min = 5.55

Example 2

下面的代码返回字符串数组中的 min()。

The following code returns min() from an array of strings.

<?php
   $arg=array("Java", "Angular", "PHP", "C", "Kotlin");
   echo "array=";
   foreach ($arg as $i) echo $i . ",";
   echo "\n";
   echo "min = " . min($arg);
?>

它将生成以下 output

It will produce the following output

array=Java,Angular,PHP,C,Kotlin,
min = Angular

Example 3

此示例中,向 min() 函数提供了一系列字符串值。

In this example, a series of string values is provided to the min() function.

<?php
   $val1="Java";
   $val2="Angular";
   $val3="PHP";
   $val4="C";
   $val5="Kotlin";
   echo "values=" . $val1 . "," . $val2 . "," . $val3 . "," . 	$val4 . "," . $val5 . "\n";
   echo "min = " . min($val1, $val2, $val3,$val4,$val5);
?>

它将生成以下 output

It will produce the following output

values=Java,Angular,PHP,C,Kotlin
min = Angular

Example 4

在此示例中,给定的数组是混合数据类型的集合。

In this example, the given array is a collection of mixed data types.

<?php
   $arg=array(23, "Java", 142, 1e2, 99);
   echo "array=";
   foreach ($arg as $i) echo $i . ",";
   echo "\n";
   echo "min = " . min($arg);
?>

它将生成以下 output

It will produce the following output

array=23,Java,142,100,99,
min = 23

PHP pow() Function

pow () 函数用于计算某个数的幂。它返回 xy 计算,也称为将 x 提升到 y。PHP 还提供了“**”作为指数运算符。

The pow () function is used to compute the power of a certain number. It returns xy calculation, also termed as x raised to y. PHP also provides "**" as exponentiation operator.

因此,pow(x,y) 返回 xy,与 x**y 相同。

So, pow(x,y) returns xy which is same as x**y.

pow ( number $base , number $exp ) : number

第一个参数是要提升的底数。第二个参数是需要将底数提升到的幂。

The first parameter is the base to be raised. The second parameter is the power to which base needs to be raised.

PHP pow() 函数返回以 exp 的幂次升高的基数。如果两个参数都是非负整数,则结果将以整数形式返回,否则以浮点数形式返回。

PHP pow() function returns the base raised to the power of exp. If both arguments are non-negative integers, the result is returned as integer, otherwise it is returned as a float.

Example 1

以下示例使用 pow() 函数计算 102:

The following example calculates 102 using pow() function −

<?php
   echo "pow(10,2) = " . pow(10,2);
   echo " using ** operator " . 10**2;
?>

它将生成以下 output

It will produce the following output

pow(10,2) = 100 using ** operator 100

Example 2

将任何数字升至 0 次方得到的结果都是 1。以下示例对此进行了验证:

Any number raised to 0 results in 1. This is verified in the following example −

<?php
   $x=10;
   $y=0;
   echo "pow(" . $x, "," . $y . ")=". pow($x,$y);
?>

它将生成以下 output

It will produce the following output

pow(10,0)=1

Example 3

以下示例展示了如何使用 pow() 函数计算 100 的平方根:

The following example shows how you can compute the square root of 100 using the pow() function −

<?php
   $x=100;
   $y=0.5;
   echo "pow(" . $x, "," . $y . ")=". pow($x,$y) . "\n";
   echo "using sqrt() function : ". sqrt(100);
?>

它将生成以下 output

It will produce the following output

pow(100,0.5)=10
using sqrt() function : 10

Example 4

本示例展示了如何使用 pow() 函数计算圆的面积。

This example shows how you can use the pow() function to calculate the area of a circle.

<?php
   $radius=5;
   echo "radius = " . $radius . " area = " . M_PI*pow(5,2);
?>

它将生成以下 output

It will produce the following output

radius = 5 area = 78.539816339745

PHP round() Function

round() 函数可用于将任意浮点数四舍五入到所需精度级别。正精度参数使数字在小数点后四舍五入;而负精度导致数字在小数点前四舍五入。精度默认为“0”。

The round() function proves useful in rounding any floating point number upto a desired precision level. Positive precision parameter causes the number to be rounded after the decimal point; whereas with negative precision, rounding occurs before the decimal point. Precision is "0" by default.

例如,round(10.6) 返回 11,round(10.2) 返回 10。此函数始终返回浮点数。

For example, round(10.6) returns 11, round(10.2) returns 10. The function always returns a floating point number.

该函数还有另一个可选参数,称为 mode ,它采用稍后描述的重新定义的常量之一。

This function also has another optional parameter called mode that takes one of the redefined constants described later.

round ( float $value , int $precision , int $mode ) : float

Parameters

  1. Value − A float number to be rounded.

  2. Precision − Number of decimal digits to round to. Default is 0. Positive precision rounds given number after decimal point. Negative precision rounds the given number before decimal point.

  3. Mode − One of the following predefined constants.

Sr.No

Constant & Description

1

PHP_ROUND_HALF_UP Rounds number away from 0 when it is half way there. Hence, 1.5 becomes 2 and -1.5 to -2

2

PHP_ROUND_HALF_DOWN Rounds number towards 0 when it is half way there. Hence 1.5 becomes 1 and -1.5 to -1

3

PHP_ROUND_HALF_EVEN Rounds the number to nearest even value

4

PHP_ROUND_HALF_ODD Rounds the number to nearest odd value

PHP round() 函数返回一个浮点数,方法是将该值四舍五入到所需的精度。

PHP round() function returns a float number that by rounding the value to a desired precision.

Example 1

以下代码将给定数字舍入到正精度值:

The following code rounds the given number to positive precision values −

<?php
   $arg=1234.567;
   echo "round(" . $arg . ") = " . round($arg) . "\n";
   echo "round(" . $arg . ",1) = " . round($arg,1) . "\n";
   echo "round(" . $arg . ",2) = " . round($arg,2) . "";
?>

它将生成以下 output

It will produce the following output

round(1234.567) = 1235
round(1234.567,1) = 1234.6
round(1234.567,2) = 1234.57

Example 2

以下代码将数字舍入到负精度值:

The following code rounds the number to negative precision values −

<?php
   $arg=1234.567;
   echo "round(" . $arg . ") = " . round($arg) . "\n";
   echo "round(" . $arg . ",-1) = " . round($arg,-1) . "\n";
   echo "round(" . $arg . ",-2) = " . round($arg,-2) . "";
?>

它将生成以下 output

It will produce the following output

round(1234.567) = 1235
round(1234.567,-1) = 1230
round(1234.567,-2) = 1200

Example 3

以下代码针对舍入使用 UP 和 DOWN 模式常量:

The following code uses UP and DOWN mode constants for rounding −

<?php
   echo "round(3.45,HALF_UP) = " . round(3.45,0, PHP_ROUND_HALF_UP) . "\n";
   echo "round(3.75 HALF_UP) = " . round(3.75, 1, PHP_ROUND_HALF_DOWN) . "";
?>

它将生成以下 output

It will produce the following output

round(3.45,HALF_UP) = 3
round(3.75 HALF_UP) = 3.7

Example 4

以下代码使用 ODD 和 EVEN 模式进行舍入 −

The following code uses ODD and EVEN modes for rounding −

<?php
   echo "round( 3.45,HALF_ODD) = " . round(3.45,0, PHP_ROUND_HALF_ODD) . "\n";
   echo "round(3.78 HALF_EVEN) = " . round(3.78, 0, PHP_ROUND_HALF_EVEN) . "";
?>

它将生成以下 output

It will produce the following output

round(3.45,HALF_ODD) = 3
round(3.78, HALF_EVEN) = 4

PHP sqrt() Function

sqrt() 函数返回正浮点数的平方根。由于负数的平方根未定义,因此它返回 NAN。这是最常用的函数之一。此函数始终返回浮点数。

The sqrt() function returns the square root of a positive float number. Since square root for a negative number is not defined, it returns NAN. This is one of the most commonly used functions. This function always returns a floating point number.

sqrt (float $arg) : float

PHP sqrt() 函数返回给定 arg 数字的平方根。对于负数,该函数返回 NAN。

PHP sqrt() function returns the square root of the given arg number. For negative numbers, the function returns NAN.

Example 1

以下代码计算 100 的平方根 −

The following code calculates the square root of 100 −

<?php
   $arg = 100;
   echo "Square root of " . $arg . "=" . sqrt($arg) . "";
?>

它将生成以下 output

It will produce the following output

Square root of 100=10

Example 2

对于 sqrt(2)、1/sqrt(2) 和 sqrt(3),PHP 分别有特殊预定义常量 M_SQRT2、M_SQRT1_2 和 M_SQRT3。

For sqrt(2), 1/sqrt(2) and sqrt(3), PHP has special predefined constants M_SQRT2, M_SQRT1_2 and M_SQRT3, respectively.

<?php
   echo "sqrt(2) = " . sqrt(2) . "\n";
   echo "M_SQRT2 = " . M_SQRT2. "\n";
   echo "sqrt(3) = " . sqrt(3) . "\n";
   echo "M_SQRT3 = " . M_SQRT3 . "\n";
   echo "1/sqrt(2)) = " . 1/sqrt(2) . "\n";
   echo "M_SQRT1_2 = " . M_SQRT1_2 . "";
?>

它将生成以下 output

It will produce the following output

sqrt(2) = 1.4142135623731
M_SQRT2 = 1.4142135623731
sqrt(3) = 1.7320508075689
M_SQRT3 = 1.7320508075689
1/sqrt(2)) = 0.70710678118655
M_SQRT1_2 = 0.70710678118655

Example 3

数学常量 M_SQRTPI 和 M_2_SQRTPI 分别表示 sqrt(Π) 和 2/sqrt(Π) 的值。

The mathematical constants M_SQRTPI and M_2_SQRTPI represent values of sqrt(Π) and 2/sqrt(Π).

<?php
   echo "sqrt(pi) = " . sqrt(M_PI) . "\n";
   echo "M_SQRTPI = " . M_SQRTPI. "\n";
   echo "2/sqrt(pi) = " . 2/sqrt(M_PI) . "\n";
   echo "M_2_SQRTPI = " . M_2_SQRTPI . "";
?>

它将生成以下 output

It will produce the following output

sqrt(pi) = 1.7724538509055
M_SQRTPI = 1.7724538509055
2/sqrt(pi) = 1.1283791670955
M_2_SQRTPI = 1.1283791670955

Example 4

sqrt(-1) 未定义,因此它返回 NAN。

sqrt(-1) is undefined, hence it returns NAN.

<?php
   echo "sqrt(-1) = " . sqrt(-1) . "";
?>

它将生成以下 output

It will produce the following output

sqrt(-1) = NAN

Predefined Mathematical Constants

除了上述数学函数外,PHP 还有以下预定义数学常量 −

In addition to the above mathematical functions, PHP also has the following list of predefined mathematical constants −

Constant

Value

Description

M_PI

3.14159265358979323846

Pi

M_E

2.7182818284590452354

Euler Number e

M_LOG2E

1.4426950408889634074

log2 e

M_LOG10E

0.43429448190325182765

log10 e

M_LN2

0.69314718055994530942

loge 2

M_LN10

M_LN10 2.30258509299404568402 loge 10

loge 10

M_PI_2

1.57079632679489661923

pi/2

M_PI_4

0.78539816339744830962

pi/4

M_1_PI

0.31830988618379067154

1/pi

M_2_PI

0.63661977236758134308

2/pi

M_SQRTPI

1.77245385090551602729

sqrt(pi)

M_2_SQRTPI

1.12837916709551257390

2/sqrt(pi)

M_SQRT2

1.41421356237309504880

sqrt(2)

M_SQRT3

1.73205080756887729352

sqrt(3)

M_SQRT1_2

0.70710678118654752440

1/sqrt(2)

M_LNPI

1.14472988584940017414

loge(pi)

M_EULER

0.57721566490153286061

Euler constant

PHP_ROUND_HALF_UP

1

Round halves up

PHP_ROUND_HALF_DOWN

2

Round halves down

PHP_ROUND_HALF_EVEN

3

Round halves to even numbers

PHP_ROUND_HALF_ODD

4

Round halves to odd numbers

NAN

NAN

Not A Number

INF

INF

Infinity