Mathematical Functions Math Introduction 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: Math constants Constant Value Description M_PI 3.14159265358979323846 Pi M_E 2.7182818284590452354 e M_LOG2E 1.4426950408889634074 log_2 e M_LOG10E 0.43429448190325182765 log_10 e M_LN2 0.69314718055994530942 log_e 2 M_LN10 2.30258509299404568402 log_e 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) [4.0.2] M_2_SQRTPI 1.12837916709551257390 2/sqrt(pi) M_SQRT2 1.41421356237309504880 sqrt(2) M_SQRT3 1.73205080756887729352 sqrt(3) [4.0.2] M_SQRT1_2 0.70710678118654752440 1/sqrt(2) M_LNPI 1.14472988584940017414 log_e(pi) [4.0.2] M_EULER 0.57721566490153286061 Euler constant [4.0.2]
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.
abs Absolute value Description mixedabs mixednumber 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). <function>abs</function> example acos Arc cosine Description floatacos floatarg Returns the arc cosine of arg in radians. See also acosh, asin and atan. acosh Inverse hyperbolic cosine Description floatacosh floatarg 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. asin Arc sine Description floatasin floatarg Returns the arc sine of arg in radians. See also asinh, acos and atan. asinh Inverse hyperbolic sine Description floatasinh floatarg 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. atan Arc tangent Description floatatan floatarg Returns the arc tangent of arg in radians. See also atanh, asin and acos. atanh Inverse hyperbolic tangent Description floatatanh floatarg 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. atan2 arc tangent of two variables Description floatatan2 floaty floatx 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_convert Convert a number between arbitrary bases Description stringbase_convert stringnumber intfrombase inttobase 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. <function>base_convert</function> bindec Binary to decimal Description intbindec stringbinary_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. ceil Round fractions up Description floatceil floatvalue 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. <function>ceil</function> examples See also floor and round. cos Cosine Description floatcos floatarg Returns the cosine of arg in radians. See also sin and tan. cosh Hyperbolic cosine Description floatcosh floatarg Returns the hyperbolic cosine of arg, defined as (exp(arg) + exp(-arg))/2. See also cos, acosh, sin and tan. decbin Decimal to binary Description stringdecbin intnumber 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. dechex Decimal to hexadecimal Description stringdechex intnumber 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. decoct Decimal to octal Description stringdecoct intnumber 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 Description floatdeg2rad floatnumber This function converts number from degrees to the radian equivalent. See also rad2deg. exp e to the power of ... Description floatexp floatarg 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 Description floatexpm1 floatnumber &warn.experimental.func; &warn.undocumented.func; floor Round fractions down Description floatfloor floatvalue 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. <function>floor</function> examples See also ceil and round. getrandmax Show largest possible random value Description intgetrandmax Returns the maximum value that can be returned by a call to rand. See also rand, srand, mt_rand, mt_srand, and mt_getrandmax. hexdec Hexadecimal to decimal Description inthexdec stringhex_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. <function>hexdec</function> example See also dechex. hypot Returns sqrt( num1*num1 + num2*num2) Description floathypot floatnum1 floatnum2 &warn.experimental.func; &warn.undocumented.func; is_finite Description boolis_finite floatval Returns &true; if val is a legal finite number within the allowed range for a PHP float on this platform. is_infinite Description boolis_infinite floatval 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_nan Description boolis_nan floatval Returns &true; if val is 'not a number', like the result of acos(1.01). lcg_value Combined linear congruential generator Description floatlcg_value lcg_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. log Natural logarithm Description floatlog floatarg Returns the natural logarithm of arg. log10 Base-10 logarithm Description floatlog10 floatarg 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 Description floatlog1p floatnumber &warn.experimental.func; &warn.undocumented.func; max Find highest value Description mixedmax mixedarg1 mixedarg2 mixedargn max 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. min Find lowest value Description numbermin numberarg1 numberarg2 ... numbermin arraynumbers min 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_rand Generate a better random value Description intmt_rand intmin intmax 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_srand Seed the better random number generator Description voidmt_srand intseed Seeds the random number generator with seed. See also mt_rand, mt_getrandmax, srand, rand, and getrandmax. mt_getrandmax Show largest possible random value Description intmt_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_format Format a number with grouped thousands Description stringnumber_format floatnumber intdecimals stringdec_point stringthousands_sep number_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. <function>number_format</function> 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. octdec Octal to decimal Description intoctdec stringoctal_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. pi Get value of pi Description floatpi 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. pow Exponential expression Description numberpow numberbase numberexp 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 <function>pow</function> ]]> 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 Description floatrad2deg floatnumber This function converts number from radian to degrees. See also deg2rad. rand Generate a random value Description intrand intmin intmax 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. round Rounds a float Description floatround floatval intprecision 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. sin Sine Description floatsin floatarg Returns the sine of arg in radians. See also cos and tan. sinh Hyperbolic sine Description floatsinh floatarg Returns the hyperbolic sine of arg, defined as (exp(arg) - exp(-arg))/2. See also sin, asinh, cos and tan. sqrt Square root Description floatsqrt floatarg Returns the square root of arg. See also pow. srand Seed the random number generator Description voidsrand intseed Seeds the random number generator with seed. See also rand, getrandmax, mt_rand, mt_srand, and mt_getrandmax. tan Tangent Description floattan floatarg Returns the tangent of arg in radians. See also sin and cos. tanh Hyperbolic tangent Description floattanh floatarg Returns the hyperbolic tangent of arg, defined as sinh(arg)/cosh(arg). See also tan, atanh, sin and cos.