Mathematical FunctionsMathIntroduction
These math functions will only handle values within the range of
the integer and float types on your computer.
(this corresponds currently to the C types long resp. double)
If you need to handle bigger numbers, take a look at the arbitrary precision math functions.
Math constants
The following values are defined as constants in PHP by the math
extension:
Only M_PI is available in PHP versions up to and including PHP 4.0.0.
All other constants are available starting with PHP 4.0.0. Constants
labeled [4.0.2] were added in PHP 4.0.2.
absAbsolute valueDescriptionmixedabsmixednumber
Returns the absolute value of number. If the argument number is
of type float, the return type is also float,
otherwise it is integer (as float usually has a
bigger value range than integer).
abs exampleacosArc cosineDescriptionfloatacosfloatarg
Returns the arc cosine of arg in radians.
See also acosh, asin
and atan.
acoshInverse hyperbolic cosineDescriptionfloatacoshfloatarg
Returns the inverse hyperbolic cosine of
arg, i.e. the value whose
hyperbolic cosine is arg.
¬e.no-windows;
See also acos, asin
and atan.
asinArc sineDescriptionfloatasinfloatarg
Returns the arc sine of arg in radians.
See also asinh, acos
and atan.
asinhInverse hyperbolic sineDescriptionfloatasinhfloatarg
Returns the inverse hyperbolic sine of
arg, i.e. the value whose
hyperbolic sine is arg.
¬e.no-windows;
See also asin, acos
and atan.
atanArc tangentDescriptionfloatatanfloatarg
Returns the arc tangent of arg in radians.
See also atanh, asin
and acos.
atanhInverse hyperbolic tangentDescriptionfloatatanhfloatarg
Returns the inverse hyperbolic tangent of
arg, i.e. the value whose
hyperbolic tangent is arg.
¬e.no-windows;
See also atan, asin
and acos.
atan2arc tangent of two variablesDescriptionfloatatan2floatyfloatx
This function calculates the arc tangent of the two variables
x and y. It is
similar to calculating the arc tangent of
y / x, except that
the signs of both arguments are used to determine the quadrant of
the result.
The function returns the result in radians, which is between -PI
and PI (inclusive).
See also acos and atan.
base_convertConvert a number between arbitrary basesDescriptionstringbase_convertstringnumberintfrombaseinttobase
Returns a string containing number
represented in base tobase. The base in
which number is given is specified in
frombase. Both
frombase and tobase
have to be between 2 and 36, inclusive. Digits in numbers with a
base higher than 10 will be represented with the letters a-z,
with a meaning 10, b meaning 11 and z meaning 35.
base_convertbindecBinary to decimalDescriptionintbindecstringbinary_string
Returns the decimal equivalent of the binary number represented by
the binary_string argument.
bindec converts a binary number to an
integer. The largest number that can be converted is
31 bits of 1's or 2147483647 in decimal.
See also the decbin
function.
ceilRound fractions upDescriptionfloatceilfloatvalue
Returns the next highest integer value by rounding up
value if necessary.
The return value of ceil is still of type
float as the value range of float is
usually bigger than that of int.
ceil examples
See also floor and
round.
cosCosineDescriptionfloatcosfloatarg
Returns the cosine of arg in radians.
See also sin and tan.
coshHyperbolic cosineDescriptionfloatcoshfloatarg
Returns the hyperbolic cosine of arg,
defined as (exp(arg) + exp(-arg))/2.
See also cos, acosh,
sin and tan.
decbinDecimal to binaryDescriptionstringdecbinintnumber
Returns a string containing a binary representation of the given
number argument. The largest number that can be converted is
4294967295 in decimal resulting to a string of 32 1's.
See also the bindec function.
dechexDecimal to hexadecimalDescriptionstringdechexintnumber
Returns a string containing a hexadecimal representation of the
given number argument. The largest number that can
be converted is 2147483647 in decimal resulting to "7fffffff".
See also hexdec.
decoctDecimal to octalDescriptionstringdecoctintnumber
Returns a string containing an octal representation of the given
number argument. The largest number that can be converted is
2147483647 in decimal resulting to "17777777777".
See also octdec.
deg2rad
Converts the number in degrees to the radian equivalent
Descriptionfloatdeg2radfloatnumber
This function converts number from degrees
to the radian equivalent.
See also rad2deg.
expe to the power of ...Descriptionfloatexpfloatarg
Returns e raised to the power of arg.
See also pow.
expm1
Returns exp(number) - 1, computed in a way that accurate even
when the value of number is close to zero
Descriptionfloatexpm1floatnumber
&warn.experimental.func;
&warn.undocumented.func;
floorRound fractions downDescriptionfloatfloorfloatvalue
Returns the next lowest integer value by rounding down
value if necessary.
The return value of floor is still of type
float because the value range of float is
usually bigger than that of int.
floor examples
See also ceil and
round.
getrandmaxShow largest possible random valueDescriptionintgetrandmax
Returns the maximum value that can be returned by a call to
rand.
See also rand, srand,
mt_rand, mt_srand, and
mt_getrandmax.
hexdecHexadecimal to decimalDescriptioninthexdecstringhex_string
Returns the decimal equivalent of the hexadecimal number
represented by the hex_string argument. hexdec converts a
hexadecimal string to a decimal number. The largest number that
can be converted is 7fffffff or 2147483647 in decimal.
hexdec will replace of any non-hexadecimal
characters it encounters by 0. This way, all
left zeros are ignored, but right zeros will be valued.
hexdec example
See also dechex.
hypot
Returns sqrt( num1*num1 + num2*num2)
Descriptionfloathypotfloatnum1floatnum2
&warn.experimental.func;
&warn.undocumented.func;
is_finiteDescriptionboolis_finitefloatval
Returns &true; if val is a legal finite
number within the allowed range for a PHP float on this platform.
is_infiniteDescriptionboolis_infinitefloatval
Returns &true; if val is infinite (positive or
negative), like the result of log(0) or any value too
big to fit into a float on this platform.
is_nanDescriptionboolis_nanfloatval
Returns &true; if val is 'not a number',
like the result of acos(1.01).
lcg_valueCombined linear congruential generatorDescriptionfloatlcg_valuelcg_value returns a pseudo random number in
the range of (0, 1). The function combines two CGs with periods
of 2^31 - 85 and 2^31 - 249. The period of this function is equal
to the product of both primes.
logNatural logarithmDescriptionfloatlogfloatarg
Returns the natural logarithm of arg.
log10Base-10 logarithmDescriptionfloatlog10floatarg
Returns the base-10 logarithm of arg.
log1p
Returns log(1 + number), computed in a way that accurate even when the val
ue of number is close to zero
Descriptionfloatlog1pfloatnumber
&warn.experimental.func;
&warn.undocumented.func;
maxFind highest valueDescriptionmixedmaxmixedarg1mixedarg2mixedargnmax returns the numerically highest of the
parameter values.
If the first parameter is an array, max
returns the highest value in that array. If the first parameter
is an integer, string or float, you need at least two parameters
and max returns the biggest of these values.
You can compare an unlimited number of values.
If one or more of the values is a float, all the values will be
treated as floats, and a float is returned. If none of the
values is a float, all of them will be treated as integers, and
an integer is returned.
minFind lowest valueDescriptionnumberminnumberarg1numberarg2...numberminarraynumbersmin returns the numerically lowest of the
parameter values.
In the first variant, you need at least two parameters
and min returns the lowest of these values.
You can compare an unlimited number of values.
In the second variant, min
returns the lowest value in numbers.
If one or more of the values is a float, all the values
will be
treated as floats, and a float is returned. If none of the
values is a float, all of them will be treated as integers,
and an integer is returned.
mt_randGenerate a better random valueDescriptionintmt_randintminintmax
Many random number generators of older libcs have dubious or
unknown characteristics and are slow. By default, PHP uses the
libc random number generator with the rand
function. mt_rand function is a drop-in
replacement for this. It uses a random number generator with
known characteristics, the Mersenne Twister, which will produce
random numbers that should be suitable for seeding some kinds
of cryptography (see the home pages for details) and is four
times faster than what the average libc provides. The Homepage
of the Mersenne Twister can be found at
&url.mersenne;, and an
optimized version of the MT source is available from
&url.mersenne.twister;
.
If called without the optional min,
max arguments mt_rand
returns a pseudo-random value between 0 and
RAND_MAX. If you want a random number
between 5 and 15 (inclusive), for example, use mt_rand
(5, 15).
Remember to seed the random number generator before use with
mt_srand.
In versions before 3.0.7 the meaning of
max was range. To
get the same results in these versions the short example should
be mt_rand (5, 11) to get a random number
between 5 and 15.
See also mt_srand,
mt_getrandmax, srand,
rand and getrandmax.
mt_srandSeed the better random number generatorDescriptionvoidmt_srandintseed
Seeds the random number generator with
seed.
See also mt_rand,
mt_getrandmax, srand,
rand, and
getrandmax.
mt_getrandmaxShow largest possible random valueDescriptionintmt_getrandmax
Returns the maximum value that can be returned by a call to
mt_rand.
See also mt_rand,
mt_srand, rand,
srand, and
getrandmax.
number_formatFormat a number with grouped thousandsDescriptionstringnumber_formatfloatnumberintdecimalsstringdec_pointstringthousands_sepnumber_format returns a formatted version of
number. This function accepts either one,
two or four parameters (not three):
If only one parameter is given,
Number will be formatted without decimals,
but with a comma (",") between every group of thousands.
If two parameters are given, number will
be formatted with decimals decimals with a
dot (".") in front, and a comma (",") between every group of
thousands.
If all four parameters are given, number
will be formatted with decimals decimals,
dec_point instead of a dot (".") before
the decimals and thousands_sep instead of
a comma (",") between every group of thousands.
Only the first character of thousands_sep
is used. For example, if you use foo as
thousands_sep on the number
1000, number_format will
return 1f000.
number_format Example
For instance, French notation usually use two decimals,
comma (',') as decimal separator, and space (' ') as
thousand separator. This is achieved with this line :
]]>
See also: sprintf,
printf and sscanf.
octdecOctal to decimalDescriptionintoctdecstringoctal_string
Returns the decimal equivalent of the octal number
represented by the octal_string argument.
The largest number that can be converted is 17777777777 or 2147483647 in
decimal.
See also decoct.
piGet value of piDescriptionfloatpi
Returns an approximation of pi. The returned float
has a precision based on the
precision directive in
&php.ini;, which defaults to 14.
Also, you can use the M_PI constant which yields
identical results to pi.
powExponential expressionDescriptionnumberpownumberbasenumberexp
Returns base raised to the power of
exp. If possible, this function will
return an integer.
If the power cannot be computed, a warning will be issued,
and pow will return &false;.
Some examples of pow
]]>
In PHP 4.0.6 and earlier pow always returned
a float, and did not issue warnings.
See also exp and sqrt.
rad2deg
Converts the radian number to the equivalent number in degrees
Descriptionfloatrad2degfloatnumber
This function converts number from radian
to degrees.
See also deg2rad.
randGenerate a random valueDescriptionintrandintminintmax
If called without the optional min,
max arguments rand
returns a pseudo-random value between 0 and
RAND_MAX. If you want a random number
between 5 and 15 (inclusive), for example, use rand (5,
15).
Remember to seed the random number generator before use with
srand.
In versions before 3.0.7 the meaning of
max was range. To
get the same results in these versions the short example should
be rand (5, 11) to get a random number
between 5 and 15.
See also srand,
getrandmax, mt_rand,
mt_srand, and
mt_getrandmax.
roundRounds a floatDescriptionfloatroundfloatvalintprecision
Returns the rounded value of val to
specified precision
(number of digits after the decimal point).
precision can also be negative or zero (default).
PHP doesn't handle strings like "12,300.2" correctly
by default. See converting from strings.
The precision parameter is only
available in PHP 4.
See also ceil and
floor.
sinSineDescriptionfloatsinfloatarg
Returns the sine of arg in radians.
See also cos and tan.
sinhHyperbolic sineDescriptionfloatsinhfloatarg
Returns the hyperbolic sine of arg,
defined as (exp(arg) - exp(-arg))/2.
See also sin, asinh,
cos and tan.
sqrtSquare rootDescriptionfloatsqrtfloatarg
Returns the square root of arg.
See also pow.
srandSeed the random number generatorDescriptionvoidsrandintseed
Seeds the random number generator with
seed.
See also rand,
getrandmax, mt_rand,
mt_srand, and
mt_getrandmax.
tanTangentDescriptionfloattanfloatarg
Returns the tangent of arg in radians.
See also sin and cos.
tanhHyperbolic tangentDescriptionfloattanhfloatarg
Returns the hyperbolic tangent of arg,
defined as sinh(arg)/cosh(arg).
See also tan, atanh,
sin and cos.