PHP之math及其函数

PHP math PHP提供了许多预定义的数学常数和函数,可用于执行数学运算。 PHP Math:abs() …

PHP math

PHP提供了许多预定义的数学常数和函数,可用于执行数学运算。

PHP Math:abs() 函数

abs()函数返回给定数字的绝对值。它返回一个整数值,但是如果您传递浮点值,它将返回一个浮点值。

number abs ( mixed $number )
<?php
echo (abs(-7)."<br/>"); // 7 (integer)
echo (abs(7)."<br/>"); //7 (integer)
echo (abs(-7.2)."<br/>"); //7.2 (float/double)
?>

输出:

7 
7 
7.2

PHP Math:ceil()函数

ceil()函数分数向上舍入。

float ceil ( float $value )
<?php
echo (ceil(3.3)."<br/>");// 4
echo (ceil(7.333)."<br/>");// 8
echo (ceil(-4.8)."<br/>");// -4
?>

输出:

4
8
-4

PHP Math:floor()函数

floor()函数分数向下舍入。

float floor ( float $value )
<?php
echo (floor(3.3)."<br/>");// 3
echo (floor(7.333)."<br/>");// 7
echo (floor(-4.8)."<br/>");// -5
?>

输出:

3
7
-5

PHP Math:sqrt() 函数

sqrt()函数返回给定参数的平方根。

float sqrt ( float $arg )
<?php
echo (sqrt(16)."<br/>");// 4
echo (sqrt(25)."<br/>");// 5
echo (sqrt(7)."<br/>");// 2.6457513110646
?>

输出:

4
5
2.6457513110646

PHP Math:decbin() 函数

decbin()函数将十进制数转换为二进制数。它以字符串返回二进制数。

string decbin ( int $number )
<?php
echo (decbin(2)."<br/>");// 10
echo (decbin(10)."<br/>");// 1010
echo (decbin(22)."<br/>");// 10110
?>

输出:

10
1010
10110

PHP Math:dechex() 函数

dechex()函数将十进制数转换为十六进制。它以字符串形式返回给定数字的十六进制表示形式。

string dechex ( int $number )
<?php
echo (dechex(2)."<br/>");// 2
echo (dechex(10)."<br/>");// a
echo (dechex(22)."<br/>");// 16
?>

输出:

2
a
16

PHP Math:decoct() 函数

decoct()函数将十进制数转换为八进制。它以字符串形式返回给定数字的八进制表示形式。

string decoct ( int $number )
<?php
echo (decoct(2)."<br/>");// 2
echo (decoct(10)."<br/>");// 12
echo (decoct(22)."<br/>");// 26
?>

输出:

2
12
26

PHP Math:base_convert() 函数

base_convert()函数允许您将任何基数转换为任何基数。例如,您可以将十六进制数转换为二进制,将十六进制转换为八进制,将二进制转换为八进制,将八进制转换为十六进制,将二进制转换为十进制等。

string base_convert ( string $number , int $frombase , int $tobase )
<?php
$n1=10;
echo (base_convert($n1,10,2)."<br/>");// 1010
?>

输出:

1010

PHP Math:bindec()函数

bindec()函数将二进制数转换为十进制数。

number bindec ( string $binary_string )
<?php
echo (bindec(10)."<br/>");// 2
echo (bindec(1010)."<br/>");// 10
echo (bindec(1011)."<br/>");// 11
?>

输出:

2
10
11

PHP数学函数

让我们看一下重要的PHP数学函数列表。

Function Description
abs() It is used to find the absolute (positive) value of a number.
sin() It is used to return the sine of a number.
sinh() It is used to return the hyperbolic sine of a number.
asin() It is used to find the arc sine of a number.
asinh() It is used to find the inverse hyperbolic sine of a number.
cos() It is used to find the cosine of a number.
cosh() It is used to return the cosh of a number.
acos() It is used to return the arc cosine of a number.
acosh() It is used to return the inverse hyperbolic cosine of a number.
tan() It is used to return the tangent of a number.
tanh() It is used to return the hyperbolic tangent of a number.
atan() It is used to return the arc tangent of a number in radians.
atan2() It is used to return the arc tangent of two variables x and y.
atanh() It is used to return the inverse hyperbolic tangent of a number.
base_convert() It is used to convert a number from one number base to another.
bindec() It is used to convert a binary number to a decimal number.
ceil() It is used to find round a number up to the nearest integer.
pi() It returns the approximation value of PI.
decbin() It converts a decimal number to a binary number.
dechex() It converts a decimal number to a hexadecimal number.
decoct() It converts a decimal number to an octal number
deg2rad() It converts a degree value to a radian value.
rad2deg() It converts a radian value to a degree value.
exp() It is used to calculate the exponent of e.
expm1() It is used to return exp(x) – 1.
floor() It converts round a number down to the nearest integer.
fmod() It returns the remainder of x/y.
getrandmax() It returns the largest possible value returned by rand().
hexadec() It is used to convert a hexadecimal number to a decimal number.
hypot() It is used to calculate the hypotenuse of a right-angle triangle.
is_finite() To check whether a value is finite or not.
is_infinite() It is used to check whether a value is infinite or not.
is_nan() It is used to check whether a value is ‘not-a-number’.
lcg_value() It is used to return a pseudo random number in a range between 0 and 1.
log() It is used to return the natural logarithm of a number.
log10() It is used to return the base-10 logarithm of a number.
log1p() It is used to return log(1+number).
max() It is used to return the highest value in an array, or the highest value of several specified values.
min() It returns the lowest value in an array, or the lowest value of several specified values.
getrandmax() It is used to return the maximum value by using rand().
mt_getrandmax() Returns the largest possible value returned by mt_rand().
mt_rand() Generates a random integer using Mersenne Twister algorithm.
mt_srand() Seeds the Mersenne Twister random number generator.
octdec() It is used to converts an octal number to a decimal number.
pow() It is used to return x raised to the power of y.
intdiv It returns the integer quotient of the division of dividend by divisor.
rand() It is used to generates a random integer.
round() It is used to round a floating-point number.
fmod() It is used to return the floating point remainder of the division of the argument.
sqrt() It is used to return the square root of a number.
类别:PHP 技巧

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册