Mathematical Functions Math. 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: Math constants Constant Value Description M_PI 3.14159265358979323846 Der Wert π (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) [CVS] M_2_SQRTPI 1.12837916709551257390 2/sqrt(pi) M_SQRT2 1.41421356237309504880 sqrt(2) M_SQRT3 1.73205080756887729352 sqrt(3) [CVS] M_SQRT1_2 0.70710678118654752440 1/sqrt(2) M_LNPI 1.14472988584940017414 log_e(pi) [CVS] M_EULER 0.57721566490153286061 Euler constant [CVS]
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.
abs Absolute value Description mixed abs mixed number Returns the absolute value of number. If the argument number is float, return type is also float, otherwise it is int. acos Arc cosine Description float acos float arg Returns the arc cosine of arg in radians. See also asin and atan. asin Arc sine Description float asin float arg Returns the arc sine of arg in radians. See also acos and atan. atan Arc tangent Description float atan float arg Returns the arc tangent of arg in radians. See also asin and acos. atan2 arc tangent of two variables Description float atan2 float y float 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_convert Convert a number between arbitrary bases Description string base_convert string number int frombase int 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. <function>Base_convert</function> $binary = base_convert ($hexadecimal, 16, 2); bindec Binary to decimal Description int bindec string 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. ceil Round fractions up Description int ceil float 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. cos Cosine Description float cos float arg Returns the cosine of arg in radians. See also sin and tan. decbin Decimal to binary Description string decbin int 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. dechex Decimal to hexadecimal Description string dechex int 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. decoct Decimal to octal Description string decoct int 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 Description double deg2rad double number This function converts number from degrees to the radian equivalent. See also rad2deg. exp e to the power of ... Description float exp float arg Returns e raised to the power of arg. See also pow. floor Round fractions down Description int floor float 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. getrandmax Show largest possible random value Description int getrandmax void 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 int hexdec string 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. log Natural logarithm Description float log float arg Returns the natural logarithm of arg. log10 Base-10 logarithm Description float log10 float arg Returns the base-10 logarithm of arg. max Find highest value Description mixed max mixed arg1 mixed arg2 mixed argn 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 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. min Find lowest value Description mixed min mixed arg1 mixed arg2 mixed argn Min 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_rand Generate a better random value Description int mt_rand int min int 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_srand Seed the better random number generator Description void mt_srand int 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_getrandmax Show largest possible random value Description int mt_getrandmax void 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 string number_format float number int decimals string dec_point string thousands_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. octdec Octal to decimal Description int octdec string 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. pi Get value of pi Description double pi void Returns an approximation of pi. pow Exponential expression Description float pow float base float exp Returns base raised to the power of exp. See also exp. rad2deg Converts the radian number to the equivalent number in degrees Description double rad2deg double number This function converts number from radian to degrees. See also deg2rad. rand Generate a random value Description int rand int min int 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. round Rounds a float Description double round double val int 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. sin Sine Description float sin float arg Returns the sine of arg in radians. See also cos and tan. sqrt Square root Description float sqrt float arg Returns the square root of arg. srand Seed the random number generator Description void srand int 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. tan Tangent Description float tan float arg Returns the tangent of arg in radians. See also sin and cos.