Mathematical Functions
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:
Table 1. Math constants
Constant |
Value |
Description |
---|---|---|
M_PI | 3.14159265358979323846 | The value of ¶ (pi) |
Mathematical Functions
Abs
Acos
Asin
Name
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.
Name
Acos — arc cosine
Description
float acos(float arg);
Returns the arc cosine of arg in radians. See also asin and atan.
Name
Asin — arc sine
Description
float asin(float arg);
Returns the arc sine of arg in radians. See also acos and atan.
Atan
Atan2
Name
Atan — arc tangent
Description
float atan(float arg);
Returns the arc tangent of arg in radians. See also acos and atan.
Name
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
Name
base_convert — convert a number between arbitrary bases
Description
strin 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.
Example 1. base_convert()
$binary = base_convert($hexadecimal, 16, 2);
BinDec
Name
BinDec — binary to decimal
Description
int bindec(string binary_string);
Ceil
Cos
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.
Name
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.
Name
Cos — cosine
Description
float cos(float arg);
Returns the cosine of arg in radians. See also sin and tan.
DecBin
Name
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
Name
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
Name
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.
Exp
Floor
Name
Exp — e to the power of...
Description
float exp(float arg);
Returns e raised to the power of arg. See also pow.
Name
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
Name
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 and srand.
HexDec
Name
HexDec — hexadecimal to decimal
Description
int hexdec(string hex_string);
Log
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.
Name
Log — natural logarithm
Description
float log(float arg);
Returns the natural logarithm of arg.
Log10
Name
Log10 — base-10 logarithm
Description
float log10(float arg);
Returns the base-10 logarithm of arg.
max
min
Name
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.
Name
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.
number_format
Name
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
Name
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
pow
rand
Name
pi — get value of pi
Description
double pi(void );
Returns an approximation of pi.
Name
pow — exponential expression
Description
float pow(float base, float exp);
Returns base raised to the power of exp. See also exp.
Name
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.
round
Name
round — Rounds a float.
Description
double round(double val);
Returns the rounded value of val.
$foo = round( 3.4 ); // $foo == 3.0
$foo = round( 3.5 ); // $foo == 4.0
$foo = round( 3.6 ); // $foo == 4.0
Sin
Sqrt
See also ceil and floor.
Name
Sin — sine
Description
float sin(float arg);
Returns the sine of arg in radians. See also cos and tan.
Name
Sqrt — square root
Description
float sqrt(float arg);
Returns the square root of arg.
srand
Tan
Name
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 and getrandmax.
Name
Tan — tangent
Description
float tan(float arg);
Returns the tangent of arg in radians. See also sin and cos.