Date post: | 12-Oct-2015 |
Category: |
Documents |
Upload: | ebooktutorials |
View: | 4,087 times |
Download: | 0 times |
of 53
PHP 5
MATH FUNCTIONS
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten TextContent Downloaded From www.w3schools.com
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
AdministratorTypewritten Text
PHP 5 Math Functions
PHP Math IntroductionThe math functions can handle values within the range of integer and float types.
InstallationThe PHP math functions are part of the PHP core. No installation is required to use these functions.
PHP 5 Math FunctionsFunction 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 ConstantsConstant 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_e10
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() FunctionExample
Return the absolute value of different numbers:
Definition and UsageThe 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() FunctionExample
Return the arc cosine of different numbers:
Definition and UsageThe 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() FunctionExample
Return the inverse hyperbolic cosine of different numbers:
Definition and UsageThe 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() FunctionExample
Return the arc sine of different numbers:
Definition and UsageThe 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() FunctionExample
Return the arc tangent of different numbers with the atan() function:
Definition and UsageThe 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() FunctionExample
Return the arc tangent of two variables with the atan2() function:
Definition and UsageThe 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() FunctionExample
Return the inverse hyperbolic tangent of different numbers:
Definition and UsageThe 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() FunctionExample
Convert a hexadecimal number to octal number:
Run example
Definition and UsageThe 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. Digitsin numbers with a base higher than 10 will be represented with the letters a-z, with a meaning10, b meaning 11 and z meaning 35
tobase Required. Specifies the base to convert to. Has to be between 2 and 36, inclusive. Digits innumbers with a base higher than 10 will be represented with the letters a-z, with a meaning10, 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:
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
Example 2
Convert an octal number to a hexadecimal number:
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP bindec() FunctionExample
Convert binary to decimal:
Definition and UsageThe 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() FunctionExample
Round numbers up to the nearest integer:
Definition and UsageThe 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() FunctionExample
Return the cosine of different numbers:
Definition and UsageThe 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() FunctionExample
Return the hyperbolic cosine of different numbers:
Definition and UsageThe 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() FunctionExample
Convert decimal to binary:
Definition and UsageThe 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() FunctionExample
Convert decimal to hexadecimal:
Definition and UsageThe 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() FunctionExample
Convert decimal to octal:
Definition and UsageThe 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() FunctionExample
Convert degrees to radians:
Definition and UsageThe 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:
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP exp() FunctionExample
Return 'e' raised to the power of different numbers:
Definition and Usage
The exp() function returns e raised to the power of x (ex).
'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() FunctionExample
Return exp() - 1:
Definition and UsageThe 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() FunctionExample
Round numbers down to the nearest integer:
Definition and UsageThe 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() FunctionExample
Return the remainder of x/y:
Definition and UsageThe 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() FunctionExample
Return largest possible random value that can be returned by rand():
Definition and UsageThe 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() FunctionExample
Convert hexadecimal to decimal:
Definition and UsageThe 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() FunctionExample
Calculate the hypotenuse of different right-angle triangles:
Definition and UsageThe 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() FunctionExample
Check whether a value is finite or not:
Definition and UsageThe 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() FunctionExample
Check whether a value is infinite or not:
Definition and UsageThe 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() FunctionExample
Check whether a value is 'not-a-number':
Definition and UsageThe 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() FunctionExample
Return a pseudo random number in a range between 0 and 1:
Definition and UsageThe 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() FunctionExample
Return largest possible random value that can be returned by mt_rand():
Definition and UsageThe 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() FunctionExample
Seed the random number generator:
Definition and UsageThe 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() FunctionExample
Generate random numbers:
Definition and UsageThe 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 FALSEif max < min
Return Type: Integer
PHP Version: 4+
PHP Changelog: PHP 4.2.0: Random number generator is seeded automaticallyPHP 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() FunctionExample
Convert octal to decimal:
Definition and UsageThe 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() FunctionExample
Return the base-10 logarithm of different numbers:
Definition and UsageThe 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() FunctionExample
Return log(1+number) for different numbers:
Definition and UsageThe log1p() function returns log(1+number), computed in a way that is accurate even when the value of number is closeto 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() FunctionExample
Return the natural logarithm of different numbers:
PHP max() FunctionExample
Find highest value with the max() function:
Definition and UsageThe 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() FunctionExample
Find lowest value with the min() function:
Definition and UsageThe 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() FunctionExample
Return the value of PI:
Definition and UsageThe 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() FunctionExample
Examples of pow():
Definition and UsageThe 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 floatPHP 4.2.0: No warning is issued on errors
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP rad2deg() FunctionExample
Convert radians to degrees:
Definition and UsageThe 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() FunctionExample
Generate random numbers:
Definition and UsageThe 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() FunctionExample
Round numbers:
Definition and UsageThe 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 halfway there. Rounds 1.5 to 2 and -1.5 to -2PHP_ROUND_HALF_DOWN - Round number down to precision decimal places, when it ishalf way there. Rounds 1.5 to 1 and -1.5 to -1PHP_ROUND_HALF_EVEN - Round number to precision decimal places towards the nexteven valuePHP_ROUND_HALF_ODD - Round number to precision decimal places towards the nextodd 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
Example 2
Round numbers using the constants:
Created By www.ebooktutorials.co.in
Content Downloaded from www.w3schools.com
PHP sin() FunctionExample
Return the sine of different numbers:
Definition and UsageThe 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() FunctionExample
Return the hyperbolic sine of different numbers:
Definition and UsageThe 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() FunctionExample
Return the square root of different numbers:
Definition and UsageThe 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() FunctionExample
Seed the random number generator:
Definition and UsageThe 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() FunctionExample
Return the tangent of different numbers:
Definition and UsageThe 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() FunctionExample
Return the hyperbolic tangent of different numbers:
Definition and UsageThe 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
w3schools.comPHP 5 Math FunctionsPHP abs() FunctionPHP acos() FunctionPHP acosh() FunctionPHP asin() FunctionPHP atan() FunctionPHP atan2() FunctionPHP atanh() FunctionPHP base_convert() FunctionPHP bindec() FunctionPHP ceil() FunctionPHP cos() FunctionPHP cosh() FunctionPHP decbin() FunctionPHP dechex() FunctionPHP decoct() FunctionPHP deg2rad() FunctionPHP exp() FunctionPHP expm1() FunctionPHP floor() FunctionPHP fmod() FunctionPHP getrandmax() FunctionPHP hexdec() FunctionPHP hypot() FunctionPHP is_finite() FunctionPHP is_infinite() FunctionPHP is_nan() FunctionPHP lcg_value() FunctionPHP mt_getrandmax() FunctionPHP mt_srand() FunctionPHP mt_rand() FunctionPHP octdec() FunctionPHP log10() FunctionPHP log1p() FunctionPHP log() FunctionPHP max() FunctionPHP min() FunctionPHP pi() FunctionPHP pow() FunctionPHP rad2deg() FunctionPHP rand() FunctionPHP round() FunctionPHP sin() FunctionPHP sinh() FunctionPHP sqrt() FunctionPHP srand() FunctionPHP tan() FunctionPHP tanh() Function