Mathematical FunctionsMath.Introduction
These math functions will only handle values within the range of
the long and double types on your computer. 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 PHP4RC1.
All other constants are available starting with PHP4.0. Constants
labelled [CVS] only available in the CVS code for PHP4.
absAbsolute valueDescriptionmixed absmixed number
Returns the absolute value of number. If the argument number is
float, return type is also float, otherwise it is int.
acosArc cosineDescriptionfloat acosfloat arg
Returns the arc cosine of arg in radians.
See also asin and atan.
asinArc sineDescriptionfloat asinfloat arg
Returns the arc sine of arg in radians.
See also acos and atan.
atanArc tangentDescriptionfloat atanfloat arg
Returns the arc tangent of arg in radians.
See also asin and acos.
atan2arc tangent of two variablesDescriptionfloat atan2float yfloat x
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 basesDescriptionstring base_convertstring numberint frombaseint tobase
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 36.
Base_convert
$binary = base_convert ($hexadecimal, 16, 2);
bindecBinary to decimalDescriptionint bindecstring binary_string
Returns the decimal equivalent of the binary number represented by
the binary_string argument.
Octdec converts a binary number to a decimal number. The largest
number that can be converted is 31 bits of 1's or 2147483647 in
decimal.
See also the decbin
function.
ceilRound fractions upDescriptionint ceilfloat number
Returns the next highest integer value from
number. Using ceil
on integers is absolutely a waste of time.
NOTE: PHP/FI 2's ceil returned a
float. Use: $new = (double)ceil($number); to
get the old behaviour.
See also floor and
round.
cosCosineDescriptionfloat cosfloat arg
Returns the cosine of arg in radians.
See also sin and tan.
decbinDecimal to binaryDescriptionstring decbinint number
Returns a string containing a binary representation of the given
number argument. The largest number that can be converted is
2147483647 in decimal resulting to a string of 31 1's.
See also the bindec function.
dechexDecimal to hexadecimalDescriptionstring dechexint number
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 the hexdec function.
decoctDecimal to octalDescriptionstring decoctint number
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
Descriptiondouble deg2raddouble number
This function converts number from degrees
to the radian equivalent.
See also rad2deg.
expe to the power of ...Descriptionfloat expfloat arg
Returns e raised to the power of arg.
See also pow.
floorRound fractions downDescriptionint floorfloat number
Returns the next lowest integer value from
number. Using floor
on integers is absolutely a waste of time.
NOTE: PHP/FI 2's floor returned a
float. Use: $new = (double)floor($number); to
get the old behaviour.
See also ceil and
round.
getrandmaxShow largest possible random valueDescriptionint getrandmaxvoid
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 decimalDescriptionint hexdecstring hex_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.
See also the dechex function.
logNatural logarithmDescriptionfloat logfloat arg
Returns the natural logarithm of arg.
log10Base-10 logarithmDescriptionfloat log10float arg
Returns the base-10 logarithm of arg.
maxFind highest valueDescriptionmixed maxmixed arg1mixed arg2mixed argnmax 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 double, 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 double, all the values will be
treated as doubles, and a double is returned. If none of the
values is a double, all of them will be treated as integers, and
an integer is returned.
minFind lowest valueDescriptionmixed minmixed arg1mixed arg2mixed argnMin returns the numerically lowest of the
parameter values.
If the first parameter is an array, min
returns the lowest value in that array. If the first parameter
is an integer, string or double, you need at least two parameters
and min returns the lowest of these values.
You can compare an unlimited number of values.
If one or more of the values is a double, all the values will be
treated as doubles, and a double is returned. If none of the
values is a double, all of them will be treated as integers, and
an integer is returned.
mt_randGenerate a better random valueDescriptionint mt_randint
minint
max
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 cryptographic purposes
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 und 15.
See also mt_srand,
mt_getrandmax, srand,
rand and getrandmax.
mt_srandSeed the better random number generatorDescriptionvoid mt_srandint seed
Seeds the random number generator with
seed.
// seed with microseconds since last "whole" second
mt_srand ((double) microtime() * 1000000);
$randval = mt_rand();
See also mt_rand,
mt_getrandmax, srand,
rand, and
getrandmax.
mt_getrandmaxShow largest possible random valueDescriptionint mt_getrandmaxvoid
Returns the maximum value that can be returned by a call to
mt_rand.
See also mt_rand,
mt_srandrand,
srand, and
getrandmax.
number_formatFormat a number with grouped thousandsDescriptionstring number_formatfloat numberint decimalsstring dec_pointstring thousands_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.
octdecOctal to decimalDescriptionint octdecstring octal_string
Returns the decimal equivalent of the octal number
represented by the octal_string argument.
OctDec converts an octal string to a decimal number. The largest
number that can be converted is 17777777777 or 2147483647 in
decimal.
See also decoct.
piGet value of piDescriptiondouble pivoid
Returns an approximation of pi.
powExponential expressionDescriptionfloat powfloat basefloat exp
Returns base raised to the power of exp.
See also exp.
rad2deg
Converts the radian number to the equivalent number in degrees
Descriptiondouble rad2degdouble number
This function converts number from radian
to degrees.
See also deg2rad.
randGenerate a random valueDescriptionint randint minint max
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 und 15.
See also srand,
getrandmax, mt_rand,
mt_srand, and
mt_getrandmax.
roundRounds a floatDescriptiondouble rounddouble valint
precision
Returns the rounded value of val to
specified precision.
$foo = round (3.4); // $foo == 3.0
$foo = round (3.5); // $foo == 4.0
$foo = round (3.6); // $foo == 4.0
$foo = round (1.95583, 2); // $foo == 1.96
See also ceil and
floor.
sinSineDescriptionfloat sinfloat arg
Returns the sine of arg in radians.
See also cos and tan.
sqrtSquare rootDescriptionfloat sqrtfloat arg
Returns the square root of arg.
srandSeed the random number generatorDescriptionvoid srandint seed
Seeds the random number generator with
seed.
// seed with microseconds since last "whole" second
srand ((double) microtime() * 1000000);
$randval = rand();
See also rand,
getrandmax, mt_rand,
mt_srand, and
mt_getrandmax.
tanTangentDescriptionfloat tanfloat arg
Returns the tangent of arg in radians.
See also sin and cos.