PHP 5 Math Functions
PHP 5 Math Functions
MATH FUNCTIONS
Content Downloaded From www.w3schools.com
PHP 5 Math Functions
PHP Math Introduction
The math functions can handle values within the range of integer and float types.
Installation
The PHP math functions are part of the PHP core. No installation is required to use these functions.
PHP 5 Math Functions
Function Description
abs() Returns the absolute (positive) value of a number
acos() Returns the arc cosine of a number
acosh() Returns the inverse hyperbolic cosine of a number
asin() Returns the arc sine of a number
asinh() Returns the inverse hyperbolic sine of a number
atan() Returns the arc tangent of a number in radians
atan2() Returns the arc tangent of two variables x and y
atanh() Returns the inverse hyperbolic tangent of a number
base_convert() Converts a number from one number base to another
bindec() Converts a binary number to a decimal number
ceil() Rounds a number up to the nearest integer
cos() Returns the cosine of a number
cosh() Returns the hyperbolic cosine of a number
decbin() Converts a decimal number to a binary number
dechex() Converts a decimal number to a hexadecimal number
decoct() Converts a decimal number to an octal number
deg2rad() Converts a degree value to a radian value
exp() Calculates the exponent of e
expm1() Returns exp(x) - 1
floor() Rounds a number down to the nearest integer
fmod() Returns the remainder of x/y
getrandmax() Returns the largest possible value returned by rand()
hexdec() Converts a hexadecimal number to a decimal number
hypot() Calculates the hypotenuse of a right-angle triangle
is_finite() Checks whether a value is finite or not
is_infinite() Checks whether a value is infinite or not
is_nan() Checks whether a value is 'not-a-number'
lcg_value() Returns a pseudo random number in a range between 0 and 1
log() Returns the natural logarithm of a number
log10() Returns the base-10 logarithm of a number
log1p() Returns log(1+number)
max() Returns the highest value in an array, or the highest value of several specified values
min() Returns the lowest value in an array, or the lowest value of several specified values
mt_getrandmax() Returns the largest possible value returned by mt_rand()
mt_rand() Generates a random integer using Mersenne Twister algorithm
mt_srand() Seeds the Mersenne Twister random number generator
octdec() Converts an octal number to a decimal number
pi() Returns the value of PI
pow() Returns x raised to the power of y
rad2deg() Converts a radian value to a degree value
rand() Generates a random integer
round() Rounds a floating-point number
sin() Returns the sine of a number
sinh() Returns the hyperbolic sine of a number
sqrt() Returns the square root of a number
srand() Seeds the random number generator
tan() Returns the tangent of a number
tanh() Returns the hyperbolic tangent of a number
PHP 5 Predefined Math Constants
Constant Value Description PHP Version
INF INF The infinite PHP 4
M_E 2.7182818284590452354 Returns e PHP 4
M_EULER 0.57721566490153286061 Returns Euler constant PHP 4
M_LNPI 1.14472988584940017414 Returns the natural logarithm of PI:
log_e(pi)
PHP 5.2
M_LN2 0.69314718055994530942 Returns the natural logarithm of 2: log_e 2 PHP 4
M_LN10 2.30258509299404568402 Returns the natural logarithm of 10: log_e
10
PHP 4
M_LOG2E 1.4426950408889634074 Returns the base-2 logarithm of E: log_2 e PHP 4
M_LOG10E 0.43429448190325182765 Returns the base-10 logarithm of E:
log_10 e
PHP 4
M_PI 3.14159265358979323846 Returns Pi PHP 4
M_PI_2 1.57079632679489661923 Returns Pi/2 PHP 4
M_PI_4 0.78539816339744830962 Returns Pi/4 PHP 4
M_1_PI 0.31830988618379067154 Returns 1/Pi PHP 4
M_2_PI 0.63661977236758134308 Returns 2/Pi PHP 4
M_SQRTPI 1.77245385090551602729 Returns the square root of PI: sqrt(pi) PHP 5.2
M_2_SQRTPI 1.12837916709551257390 Returns 2/square root of PI: 2/sqrt(pi) PHP 4
M_SQRT1_2 0.70710678118654752440 Returns the square root of 1/2: 1/sqrt(2) PHP 4
M_SQRT2 1.41421356237309504880 Returns the square root of 2: sqrt(2) PHP 4
M_SQRT3 1.73205080756887729352 Returns the square root of 3: sqrt(3) PHP 5.2
NAN NAN Not A Number PHP 4
PHP_ROUND_HALF_UP 1 Round halves up PHP 5.3
PHP_ROUND_HALF_DOWN 2 Round halves down PHP 5.3
PHP_ROUND_HALF_EVEN 3 Round halves to even numbers PHP 5.3
PHP_ROUND_HALF_ODD 4 Round halves to odd numbers PHP 5.3
PHP abs() Function
Example
Return the absolute value of different numbers:
<?php
echo(abs(6.7) . "<br>");
echo(abs(-6.7) . "<br>");
echo(abs(-3) . "<br>");
echo(abs(3));
?>
Definition and Usage
The abs() function returns the absolute (positive) value of a number.
Syntax
abs(number);
Parameter Description
number Required. Specifies a number. If the number is of type float, the return type is also float,
otherwise it is integer
Technical Details
Return Value: The absolute value of the number
Return Type: Float / Integer
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP acos() Function
Example
Return the arc cosine of different numbers:
<?php
echo(acos(0.64) . "<br>");
echo(acos(-0.4) . "<br>");
echo(acos(0) . "<br>");
echo(acos(-1) . "<br>");
echo(acos(1) . "<br>");
echo(acos(2));
?>
Definition and Usage
The acos() function returns the arc cosine of a number.
Tip: acos(-1) returns the value of Pi.
Syntax
acos(number);
Parameter Description
number Required. Specifies a number in range -1 to 1
Technical Details
Return Value: The arc cosine of a number. Returns NAN if number is not in the range -1 to 1
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP acosh() Function
Example
Return the inverse hyperbolic cosine of different numbers:
<?php
echo(acosh(7) . "<br>");
echo(acosh(56) . "<br>");
echo(acosh(2.45));
?>
Definition and Usage
The acosh() function returns the inverse hyperbolic cosine of a number.
Syntax
acosh(number);
Parameter Description
number Required. Specifies a number
Technical Details
Return Value: The inverse hyperbolik cosine of number
Return Type: Float
PHP Version: 4.1+
PHP Changelog: PHP 5.3: acosh() is now available on all platform
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP asin() Function
Example
Return the arc sine of different numbers:
<?php
echo(asin(0.64) . "<br>");
echo(asin(-0.4) . "<br>");
echo(asin(0) . "<br>");
echo(asin(-1) . "<br>");
echo(asin(1) . "<br>");
echo(asin(2));
?>
Definition and Usage
The asin() function returns the arc sine of a number.
Tip: asin(1) returns the value of Pi/2.
Syntax
asin(number);
Parameter Description
number Required. Specifies a number in range -1 to 1
Technical Details
Return Value: The arc sine of a number. Returns NAN if number is not in the range -1 to 1
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP atan() Function
Example
Return the arc tangent of different numbers with the atan() function:
<?php
echo(atan(0.50) . "<br>");
echo(atan(-0.50) . "<br>");
echo(atan(5) . "<br>");
echo(atan(-5) . "<br>");
echo(atan(100) . "<br>");
echo(atan(-100));
?>
Definition and Usage
The atan() function returns the arc tangent of arg as a numeric value between -Pi/2 and Pi/2 radians.
Syntax
atan(arg);
Parameter Description
arg Required. Specifies an argument to process
Technical Details
Return Value: The arc tangent of arg in radians
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP atan2() Function
Example
Return the arc tangent of two variables with the atan2() function:
<?php
echo(atan2(0.50,0.50) . "<br>");
echo(atan2(-0.50,-0.50) . "<br>");
echo(atan2(5,5) . "<br>");
echo(atan2(10,20) . "<br>");
echo(atan2(-5,-5) . "<br>");
echo(atan2(-10,10));
?>
Definition and Usage
The atan2() function returns the arc tangent of two variables x and y.
Syntax
atan2(y,x);
Parameter Description
y Required. Specifies the dividend
x Required. Specifies the divisor
Technical Details
Return Value: The arc tangent of y/x in radians, which is between -Pi and Pi
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP atanh() Function
Example
Return the inverse hyperbolic tangent of different numbers:
<?php
echo(atanh(M_PI_4) . "<br>");
echo(atanh(0.50) . "<br>");
echo(atanh(-0.50) . "<br>");
echo(atanh(1) . "<br>");
echo(atanh(-1));
?>
Definition and Usage
The atanh() function returns the inverse hyperbolic tangent of a number.
Syntax
atanh(number);
Parameter Description
number Required. Specifies a number
Technical Details
Return Value: The hyperbolic tangent of number
Return Type: Float
PHP Version: 4.1+
PHP Changelog: PHP 5.3: This function is now available on all platforms
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP base_convert() Function
Example
Convert a hexadecimal number to octal number:
<?php
$hex = "E196";
echo base_convert($hex,16,8);
?>
Run example
Definition and Usage
The base_convert() function converts a number from one number base to another.
Syntax
base_convert(number,frombase,tobase);
Parameter Description
number Required. Specifies the number to convert
frombase Required. Specifies the original base of number. Has 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
tobase Required. Specifies the base to convert to. Has 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
Technical Details
Return Value: The number converted to the specified base
Return Type: String
PHP Version: 4+
Example 1
Convert an octal number to a decimal number:
<?php
$oct = "0031";
echo base_convert($oct,8,10);
?>
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
Example 2
Convert an octal number to a hexadecimal number:
<?php
$oct = "364";
echo base_convert($oct,8,16);
?>
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP bindec() Function
Example
Convert binary to decimal:
<?php
echo bindec("0011") . "<br>";
echo bindec("01") . "<br>";
echo bindec("11000110011") . "<br>";
echo bindec("111");
?>
Definition and Usage
The bindec() function converts a binary number to a decimal number.
Tip: To convert decimal to binary, look at the decbin() function.
Syntax
bindec(binary_string);
Parameter Description
binary_string Required. Specifies the binary string to convert.
Note: The parameter value must be a string!
Technical Details
Return Value: The decimal value of binary_string
Return Type: Float / Integer
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP ceil() Function
Example
Round numbers up to the nearest integer:
<?php
echo(ceil(0.60) . "<br>");
echo(ceil(0.40) . "<br>");
echo(ceil(5) . "<br>");
echo(ceil(5.1) . "<br>");
echo(ceil(-5.1) . "<br>");
echo(ceil(-5.9));
?>
Definition and Usage
The ceil() function rounds a number UP to the nearest integer, if necessary.
Tip: To round a number DOWN to the nearest integer, look at the floor() function.
Tip: To round a floating-point number, look at the round() function.
Syntax
ceil(number);
Parameter Description
number Required. Specifies the value to round up
Technical Details
Return Value: The value rounded up to the nearest integer
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP cos() Function
Example
Return the cosine of different numbers:
<?php
echo(cos(3) . "<br>");
echo(cos(-3) . "<br>");
echo(cos(0) . "<br>");
echo(cos(M_PI) . "<br>");
echo(cos(2*M_PI));
?>
Definition and Usage
The cos() function returns the cosine of a number.
Note: The cos() function returns a numeric value between -1 and 1, which represents the cosine of the angle.
Syntax
cos(number);
Parameter Description
number Required. Specifies a number
Technical Details
Return Value: The cosine of number
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP cosh() Function
Example
Return the hyperbolic cosine of different numbers:
<?php
echo(cosh(3) . "<br>");
echo(cosh(-3) . "<br>");
echo(cosh(0) . "<br>");
echo(cosh(M_PI) . "<br>");
echo(cosh(2*M_PI));
?>
Definition and Usage
The cosh() function returns the hyperbolic cosine of a number (equivalent to (exp(number) + exp(-number)) / 2).
Syntax
cosh(number);
Parameter Description
number Required. Specifies a number
Technical Details
Return Value: The hyperbolic cosine of number
Return Type: Float
PHP Version: 4.1+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP decbin() Function
Example
Convert decimal to binary:
<?php
echo decbin("3") . "<br>";
echo decbin("1") . "<br>";
echo decbin("1587") . "<br>";
echo decbin("7");
?>
Definition and Usage
The decbin() function converts a decimal number to a binary number.
Tip: To convert binary to decimal, look at the bindec() function.
Syntax
decbin(number);
Parameter Description
number Required. Specifies the decimal value to convert
Technical Details
Return Value: A string that contains the binary number of the decimal value
Return Type: String
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP dechex() Function
Example
Convert decimal to hexadecimal:
<?php
echo dechex("30") . "<br>";
echo dechex("10") . "<br>";
echo dechex("1587") . "<br>";
echo dechex("70");
?>
Definition and Usage
The dechex() function converts a decimal number to a hexadecimal number.
Tip: To convert hexadecimal to decimal, look at the hexdec() function.
Syntax
dechex(number);
Parameter Description
number Required. Specifies the decimal value to convert
Technical Details
Return Value: A string that contains the hexadecimal number of the decimal value
Return Type: String
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP decoct() Function
Example
Convert decimal to octal:
<?php
echo decoct("30") . "<br>";
echo decoct("10") . "<br>";
echo decoct("1587") . "<br>";
echo decoct("70");
?>
Definition and Usage
The decoct() function converts a decimal number to an octal number.
Tip: To convert octal to decimal, look at the octdec() function.
Syntax
decoct(number);
Parameter Description
number Required. Specifies the decimal value to convert
Technical Details
Return Value: A string that contains the octal number of the decimal value
Return Type: String
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP deg2rad() Function
Example
Convert degrees to radians:
<?php
echo deg2rad("45") . "<br>";
echo deg2rad("90") . "<br>";
echo deg2rad("360");
?>
Definition and Usage
The deg2rad() function converts a degree value to a radian value.
Tip: To convert a radian value to a degree value, look at the rad2deg() function.
Syntax
deg2rad(number);
Parameter Description
number Required. Specifies the degree to convert
Technical Details
Return Value: The radian equivalent of number
Return Type: Float
PHP Version: 4+
Example 1
Convert a degree into its radian equivalent:
<?php
$deg = 180;
$rad = deg2rad($deg);
echo "$deg degrees is equal to $rad radians.";
?>
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP exp() Function
Example
Return 'e' raised to the power of different numbers:
<?php
echo(exp(0) . "<br>");
echo(exp(1) . "<br>");
echo(exp(10) . "<br>");
echo(exp(4.8));
?>
Definition and Usage
The exp() function returns e raised to the power of x (e
x
).
'e' is the base of the natural system of logarithms (approximately 2.718282) and x is the number passed to it.
Syntax
exp(x);
Parameter Description
x Required. Specifies the exponent
Technical Details
Return Value: 'e' raised to the power of x
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP expm1() Function
Example
Return exp() - 1:
<?php
echo(expm1(0) . "<br>");
echo(expm1(1) . "<br>");
echo(expm1(10) . "<br>");
echo(expm1(4.8));
?>
Definition and Usage
The expm1() function returns exp(x) - 1.
Syntax
expm1(x);
Parameter Description
x Required. Specifies the exponent
Technical Details
Return Value: 'e' raised to the power of x minus 1
Return Type: Float
PHP Version: 4.1+
PHP Changelog: PHP 5.3: expm1() now available on all platforms
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP floor() Function
Example
Round numbers down to the nearest integer:
<?php
echo(floor(0.60) . "<br>");
echo(floor(0.40) . "<br>");
echo(floor(5) . "<br>");
echo(floor(5.1) . "<br>");
echo(floor(-5.1) . "<br>");
echo(floor(-5.9));
?>
Definition and Usage
The floor() function rounds a number DOWN to the nearest integer, if necessary.
Tip: To round a number UP to the nearest integer, look at the ceil() function.
Tip: To round a floating-point number, look at the round() function.
Syntax
floor(number);
Parameter Description
number Required. Specifies the value to round down
Technical Details
Return Value: The value rounded down to the nearest integer
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP fmod() Function
Example
Return the remainder of x/y:
<?php
$x = 7;
$y = 2;
$result = fmod($x,$y);
echo $result;
// $result equals 1, because 2 * 3 + 1 = 7
?>
Definition and Usage
The fmod() function returns the remainder (modulo) of x/y.
Syntax
fmod(x,y);
Parameter Description
x Required. Specifies the dividend
y Required. Specifies the divisor
Technical Details
Return Value: The floating point remainder of x/y
Return Type: Float
PHP Version: 4.2+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP getrandmax() Function
Example
Return largest possible random value that can be returned by rand():
<?php
echo(getrandmax());
?>
Definition and Usage
The getrandmax() function returns the largest possible value that can be returned by rand().
Syntax
getrandmax();
Technical Details
Return Value: The largest possible value returned by rand()
Return Type: Integer
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP hexdec() Function
Example
Convert hexadecimal to decimal:
<?php
echo hexdec("1e") . "<br>";
echo hexdec("a") . "<br>";
echo hexdec("11ff") . "<br>";
echo hexdec("cceeff");
?>
Definition and Usage
The hexdec() function converts a hexadecimal number to a decimal number.
Tip: To convert decimal to hexadecimal, look at the dechex() function.
Syntax
hexdec(hex_string);
Parameter Description
hex_string Required. Specifies the hexadecimal string to convert
Technical Details
Return Value: The decimal value of hex_string
Return Type: Float / Integer
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP hypot() Function
Example
Calculate the hypotenuse of different right-angle triangles:
<?php
echo hypot(3,4) . "<br>";
echo hypot(4,6) . "<br>";
echo hypot(1,3) . "<br>";
echo sqrt(3*3+4*4);
?>
Definition and Usage
The hypot() function calculates the hypotenuse of a right-angle triangle.
Tip: This function is equivalent to sqrt(x*x + y*y).
Syntax
hypot(x,y);
Parameter Description
x Required. Specifies the length of first side
y Required. Specifies the length of second side
Technical Details
Return Value: The length of the hypotenuse
Return Type: Float
PHP Version: 4.1+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP is_finite() Function
Example
Check whether a value is finite or not:
<?php
echo is_finite(2) . "<br>";
echo is_finite(log(0)) . "<br>";
echo is_finite(2000);
?>
Definition and Usage
The is_finite() function checks whether a value is finite or not.
This function returns true (1) if the specified value is a finite number, otherwise it returns false/nothing.
The value is finite if it is within the allowed range for a PHP float on this platform.
Syntax
is_finite(value);
Parameter Description
value Required. Specifies the value to check
Technical Details
Return Value: TRUE if value is a finite number, FALSE otherwise
Return Type: Boolean
PHP Version: 4.2+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP is_infinite() Function
Example
Check whether a value is infinite or not:
<?php
echo is_infinite(2) . "<br>";
echo is_infinite(log(0)) . "<br>";
echo is_infinite(2000);
?>
Definition and Usage
The is_infinite() function checks whether a value is infinite or not.
This function returns true (1) if the specified value is an infinite number, otherwise it returns false/nothing.
The value is infinite if it is outside the allowed range for a PHP float on this platform.
Syntax
is_infinite(value);
Parameter Description
value Required. Specifies the value to check
Technical Details
Return Value: TRUE if value is an infinite number, FALSE otherwise
Return Type: Boolean
PHP Version: 4.2+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP is_nan() Function
Example
Check whether a value is 'not-a-number':
<?php
echo is_nan(200) . "<br>";
echo is_nan(acos(1.01));
?>
Definition and Usage
The is_nan() function checks whether a value is 'not a number'.
This function returns true (1) if the specified value is 'not a number', otherwise it returns false/nothing.
Syntax
is_nan(value);
Parameter Description
value Required. Specifies the value to check
Technical Details
Return Value: TRUE if value is 'not a number', FALSE otherwise
Return Type: Boolean
PHP Version: 4.2+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP lcg_value() Function
Example
Return a pseudo random number in a range between 0 and 1:
<?php
echo lcg_value();
?>
Definition and Usage
The lcg_value() function returns a pseudo random number in a range between 0 and 1.
Syntax
lcg_value();
Technical Details
Return Value: A pseudo random float value in a range between 0 and 1
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP mt_getrandmax() Function
Example
Return largest possible random value that can be returned by mt_rand():
<?php
echo(mt_getrandmax());
?>
Definition and Usage
The mt_getrandmax() function returns the largest possible value that can be returned by mt_rand().
Syntax
mt_getrandmax();
Technical Details
Return Value: The largest possible value returned by mt_rand()
Return Type: Integer
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP mt_srand() Function
Example
Seed the random number generator:
<?php
mt_srand(mktime());
echo(mt_rand());
?>
Definition and Usage
The mt_srand() function seeds the Mersenne Twister random number generator.
Tip: From PHP 4.2.0, the random number generator is seeded automatically and there is no need to use this function.
Syntax
mt_srand(seed);
Parameter Description
seed Optional. Specifies the seed value
Technical Details
Return Value: None
Return Type: -
PHP Version: 4+
PHP Changelog: PHP 4.2.0: Random number generator is now seeded automatically
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP mt_rand() Function
Example
Generate random numbers:
<?php
echo(mt_rand() . "<br>");
echo(mt_rand() . "<br>");
echo(mt_rand(10,100));
?>
Definition and Usage
The mt_rand() function generates a random integer using the Mersenne Twister algorithm.
Tip: This function produces a better random value, and is 4 times faster than rand().
Tip: If you want a random integer between 10 and 100 (inclusive), use mt_rand (10,100).
Syntax
mt_rand();
or
mt_rand(min,max);
Parameter Description
min Optional. Specifies the lowest number to be returned. Default is 0
max Optional. Specifies the highest number to be returned. Default is mt_getrandmax()
Technical Details
Return Value: A random integer between min (or 0) and max (or mt_getrandmax() inclusive). Returns FALSE
if max < min
Return Type: Integer
PHP Version: 4+
PHP Changelog: PHP 4.2.0: Random number generator is seeded automatically
PHP 5.3.4: Issues an E_WARNING and returns FALSE if max < min
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP octdec() Function
Example
Convert octal to decimal:
<?php
echo octdec("36") . "<br>";
echo octdec("12") . "<br>";
echo octdec("3063") . "<br>";
echo octdec("106");
?>
Definition and Usage
The octdec() function converts an octal number to a decimal number.
Tip: To convert decimal to octal, look at the decoct() function.
Syntax
octdec(octal_string);
Parameter Description
octal_string Required. Specifies the octal string to convert
Technical Details
Return Value: The decimal value of octal_string
Return Type: Float / Integer
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP log10() Function
Example
Return the base-10 logarithm of different numbers:
<?php
echo(log10(2.7183) . "<br>");
echo(log10(2) . "<br>");
echo(log10(1) . "<br>");
echo(log10(0));
?>
Definition and Usage
The log10() function returns the base-10 logarithm of a number.
Syntax
log10(number);
Parameter Description
number Required. Specifies the value to calculate the logarithm for
Technical Details
Return Value: The base-10 logarithm of number
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP log1p() Function
Example
Return log(1+number) for different numbers:
<?php
echo(log1p(2.7183) . "<br>");
echo(log1p(2) . "<br>");
echo(log1p(1) . "<br>");
echo(log1p(0));
?>
Definition and Usage
The log1p() function returns log(1+number), computed in a way that is accurate even when the value of number is close
to zero.
Syntax
log1p(number);
Parameter Description
number Required. Specifies the number to process
Technical Details
Return Value: log(1+number)
Return Type: Float
PHP Version: 4.1+
PHP Changelog: PHP 5.3: The log1p() function is now available on all platforms
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP log() Function
Example
Return the natural logarithm of different numbers:
<?php
echo(log(2.7183) . "<br>");
echo(log(2) . "<br>");
echo(log(1) . "<br>");
echo(log(0);
?>
Definition and Usage
The log() function returns the natural logarithm of a number, or the logarithm of number to base.
Syntax
log(number,base);
Parameter Description
number Required. Specifies the value to calculate the logarithm for
base Optional. The logarithmic base to use. Default is 'e'
Technical Details
Return Value: The natural logarithm of a number, or the logarithm of number to base
Return Type: Float
PHP Version: 4+
PHP Changelog: PHP 4.3: The base parameter were added
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP max() Function
Example
Find highest value with the max() function:
<?php
echo(max(2,4,6,8,10) . "<br>");
echo(max(22,14,68,18,15) . "<br>");
echo(max(array(4,6,8,10)) . "<br>");
echo(max(array(44,16,81,12)));
?>
Definition and Usage
The max() function returns the highest value in an array, or the highest value of several specified values.
Syntax
max(array_values);
or
max(value1,value2,...);
Parameter Description
array_values Required. Specifies an array containing the values
value1,value2,... Required. Specifies the values to compare (must be at least two values)
Technical Details
Return Value: The numerically highest value
Return Type: Mixed
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP min() Function
Example
Find lowest value with the min() function:
<?php
echo(min(2,4,6,8,10) . "<br>");
echo(min(22,14,68,18,15) . "<br>");
echo(min(array(4,6,8,10)) . "<br>");
echo(min(array(44,16,81,12)));
?>
Definition and Usage
The min() function returns the lowest value in an array, or the lowest value of several specified values.
Syntax
min(array_values);
or
min(value1,value2,...);
Parameter Description
array_values Required. Specifies an array containing the values
value1,value2,... Required. Specifies the values to compare (must be at least two values)
Technical Details
Return Value: The numerically lowest value
Return Type: Mixed
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP pi() Function
Example
Return the value of PI:
<?php
echo(pi());
?>
Definition and Usage
The pi() function returns the value of PI.
Tip: The named constant M_PI is identical to pi().
Syntax
pi();
Technical Details
Return Value: 3.1415926535898
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP pow() Function
Example
Examples of pow():
<?php
echo(pow(2,4) . "<br>");
echo(pow(-2,4) . "<br>");
echo(pow(-2,-4) . "<br>");
echo(pow(-2,-3.2));
?>
Definition and Usage
The pow() function returns x raised to the power of y.
Syntax
pow(x,y);
Parameter Description
x Required. Specifies the base to use
y Required. Specifies the exponent
Technical Details
Return Value: x raised to the power of y
Return Type: Integer if possible, Float otherwise
PHP Version: 4+
PHP Changelog: PHP 4.0.6: Will now return integer if possible. Earlier it only returned float
PHP 4.2.0: No warning is issued on errors
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP rad2deg() Function
Example
Convert radians to degrees:
<?php
echo rad2deg(pi()) . "<br>";
echo rad2deg(pi()/4);
?>
Definition and Usage
The rad2deg() function converts a radian value to a degree value.
Tip: To convert a degree value to a radian value, look at the deg2rad() function.
Syntax
rad2deg(number);
Parameter Description
number Required. Specifies a radian value to convert
Technical Details
Return Value: The equivalent of number in degrees
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP rand() Function
Example
Generate random numbers:
<?php
echo(rand() . "<br>");
echo(rand() . "<br>");
echo(rand(10,100));
?>
Definition and Usage
The rand() function generates a random integer.
Tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100).
Tip: The mt_rand() function produces a better random value, and is 4 times faster than rand().
Syntax
rand();
or
rand(min,max);
Parameter Description
min Optional. Specifies the lowest number to be returned. Default is 0
max Optional. Specifies the highest number to be returned. Default is getrandmax()
Technical Details
Return Value: A random integer between min (or 0) and max (or getrandmax() inclusive)
Return Type: Integer
PHP Version: 4+
PHP Changelog: PHP 4.2.0: Random number generator is seeded automatically
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP round() Function
Example
Round numbers:
<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
Definition and Usage
The round() function rounds a floating-point number.
Tip: To round a number UP to the nearest integer, look at the ceil() function.
Tip: To round a number DOWN to the nearest integer, look at the floor() function.
Syntax
round(number,precision,mode);
Parameter Description
number Required. Specifies the value to round
precision Optional. Specifies the number of decimal digits to round to. Default is 0
mode Optional. Specifies a constant to specify the rounding mode:
PHP_ROUND_HALF_UP - Default. Rounds number up to precision decimal, when it is half
way there. Rounds 1.5 to 2 and -1.5 to -2
PHP_ROUND_HALF_DOWN - Round number down to precision decimal places, when it is
half way there. Rounds 1.5 to 1 and -1.5 to -1
PHP_ROUND_HALF_EVEN - Round number to precision decimal places towards the next
even value
PHP_ROUND_HALF_ODD - Round number to precision decimal places towards the next
odd value
Technical Details
Return Value: The rounded value
Return Type: Float
PHP Version: 4+
PHP Changelog: PHP 5.3: The mode parameter was added
Example 1
Round numbers to two decimals:
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
<?php
echo(round(4.96754,2) . "<br>");
echo(round(7.045,2) . "<br>");
echo(round(7.055,2));
?>
Example 2
Round numbers using the constants:
<?php
echo(round(1.5,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_ODD) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_ODD));
?>
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP sin() Function
Example
Return the sine of different numbers:
<?php
echo(sin(3) . "<br>");
echo(sin(-3) . "<br>");
echo(sin(0) . "<br>");
echo(sin(M_PI) . "<br>");
echo(sin(M_PI_2));
?>
Definition and Usage
The sin() function returns the sine of a number.
Syntax
sin(number);
Parameter Description
number Required. Specifies a value in radians
Technical Details
Return Value: The sine of number
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP sinh() Function
Example
Return the hyperbolic sine of different numbers:
<?php
echo(sinh(3) . "<br>");
echo(sinh(-3) . "<br>");
echo(sinh(0) . "<br>");
echo(sinh(M_PI) . "<br>");
echo(sinh(M_PI_2));
?>
Definition and Usage
The sinh() function returns the hyperbolic sine of a number, which is equal to (exp(number) - exp(-number))/2).
Syntax
sinh(number);
Parameter Description
number Required. Specifies a number
Technical Details
Return Value: The hyperbolic sine of number
Return Type: Float
PHP Version: 4.1+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP sqrt() Function
Example
Return the square root of different numbers:
<?php
echo(sqrt(0) . "<br>");
echo(sqrt(1) . "<br>");
echo(sqrt(9) . "<br>");
echo(sqrt(0.64) . "<br>");
echo(sqrt(-9));
?>
Definition and Usage
The sqrt() function returns the square root of a number.
Syntax
sqrt(number);
Parameter Description
number Required. Specifies a number
Technical Details
Return Value: The square root of number, or NAN for negative numbers
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP srand() Function
Example
Seed the random number generator:
<?php
srand(mktime());
echo(rand());
?>
Definition and Usage
The srand() function seeds the random number generator (rand()).
Tip: From PHP 4.2.0, the random number generator is seeded automatically and there is no need to use this function.
Syntax
srand(seed);
Parameter Description
seed Optional. Specifies the seed value
Technical Details
Return Value: None
Return Type: -
PHP Version: 4+
PHP Changelog: PHP 4.2.0: Random number generator is now seeded automatically
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP tan() Function
Example
Return the tangent of different numbers:
<?php
echo(tan(M_PI_4) . "<br>");
echo(tan(0.50) . "<br>");
echo(tan(-0.50) . "<br>");
echo(tan(5) . "<br>");
echo(tan(10) . "<br>");
echo(tan(-5) . "<br>");
echo(tan(-10));
?>
Definition and Usage
The tan() function returns the tangent of a number.
Syntax
tan(number);
Parameter Description
number Required. Specifies a value in radians
Technical Details
Return Value: The tangent of number
Return Type: Float
PHP Version: 4+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP tanh() Function
Example
Return the hyperbolic tangent of different numbers:
<?php
echo(tanh(M_PI_4) . "<br>");
echo(tanh(0.50) . "<br>");
echo(tanh(-0.50) . "<br>");
echo(tanh(5) . "<br>");
echo(tanh(10) . "<br>");
echo(tanh(-5) . "<br>");
echo(tanh(-10));
?>
Definition and Usage
The tanh() function returns the hyperbolic tangent of a number, which is equal to sinh(x)/cosh(x).
Syntax
tanh(number);
Parameter Description
number Required. Specifies a number
Technical Details
Return Value: The hyperbolic tangent of number
Return Type: Float
PHP Version: 4.1+
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com