PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
Last Updated :
18 Jan, 2022
11. is_nan() :
This function takes any value as argument and checks whether a value is number or not. It returns TRUE (1) if the specified value is 'not a number', otherwise it returns FALSE/NOTHING.
Syntax :
is_nan(value);
Example :
PHP
<?php
echo is_nan(234) . "\n";
echo is_nan(asin(1.1)) ;
?>
Output :
1
Note : (is_finite(value)) is equivalent to (!is_infinite(value) && (!is_nan(value)), i.e. a number can only be one of finite, infinite and NaN(not a number). You don't need to check both is_infinite() and is_nan() to see if a number is invalid or out of range.
12. pow() :
This function takes base and exponent as arguments and returns base raised to the power of exponent.
Syntax :
pow(base,exponent);
Example :
PHP
<?php
echo(pow(2,4) . "\n");
echo(pow(-2,-4));
?>
Output :
16
0.0625
13. sqrt() :
This function takes numeric value as arguments and returns the square root of value.
Syntax :
sqrt(number);
Example :
PHP
<?php
echo(sqrt(9) . "\n");
echo(sqrt(0.64));
?>
Output :
3
0.8
14. exp() :
The function returns e raised to the power of 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);
Example :
PHP
<?php
# PHP code to calculate exponent of e.
function exponent($number){
return (exp($number));
}
// Driver Code
$number = 0;
echo (exponent($number));
?>
Output :
1
15. log() :
This function takes any number and base as arguments and returns the natural logarithm of a number, or the logarithm of number to base.
Syntax :
log(number, base);
In this, number specifies the value to calculate the logarithm for.
base(Optional) specifies the logarithmic base to use (Default is 'e').
Example :
PHP
<?php
echo(log(5.987) . "\n");
echo(log(1));
?>
Output :
1.7895904519432
0
16. log10() :
This function takes any number as argument and returns the base-10 logarithm of a number.
Syntax :
log10(number);
Here, number specifies the value to calculate the logarithm for.
Example :
PHP
<?php
echo(log10(5.987) . "\n");
echo(log10(0));
?>
Output :
0.77720925814568
-INF
17. log1p() :
This function takes any number as argument and returns log(1+number), computed in a way that is accurate even when the value of number is close to zero.
Syntax :
log1p(number);
Here, number specifies the number to process.
Example :
PHP
<?php
echo(log1p(5.987) . "\n");
echo(log1p(0));
?>
Output :
1.9440512795703
0
18. max() :
In this function, if the first and only parameter is an array, max() returns the highest value in that array. If at least two parameters are provided, max() returns the biggest of these values.
Syntax :
max(array_values);
or
max(value1,value2,...);
Example :
PHP
<?php
echo(max(3,4,6,12,10) . "\n");
echo(max(array(48,76,83,22)));
?>
Output :
12
83
19. min() :
In this function, if the first and only parameter is an array, min() returns the lowest value in that array. If at least two parameters are provided, min() returns the smallest of these values.
Syntax :
min(array_values);
or
min(value1,value2,...);
Example :
PHP
<?php
echo(min(3,4,6,12,10) . "\n");
echo(min(array(48,76,83,22)));
?>
Output :
3
22
20. getrandmax() :
This function doesn't take any arguments and returns the largest possible value that can be returned by rand() function.
Syntax :
getrandmax();
Example :
PHP
<?php
echo(getrandmax());
?>
Output :
2147483647
21. rand() :
If this function is called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 12 and 56 (inclusive). Example, use rand(12, 56).
Syntax :
rand();
or
rand(min,max);
Example :
Here, min Specifies the lowest number to be returned(Default is 0).
max Specifies the highest number to be returned(Default is getrandmax())
PHP
<?php
echo(rand() . "\n");
echo(rand(12,100));
?>
Output :
1135079733
76
Output may vary time to time as it generates random number.
22. mt_rand() :
This function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the Mersenne Twister algorithm, which will produce random numbers four times faster than what the average rand() provides.
If called without the optional min, max arguments mt_rand() returns a pseudo-random value between 0 and mt_getrandmax(). If you want a random number between 12 and 56 (inclusive),Example, use mt_rand(12, 56).
Syntax :
mt_rand();
or
mt_rand(min,max);
Example :
Here, min Specifies the lowest number to be returned(Default is 0).
max Specifies the highest number to be returned(Default is getrandmax())
PHP
<?php
echo(mt_rand() . "\n");
echo(mt_rand(12,100));
?>
Output :
952458556
87
Output may vary time to time as it generates random number.
Similar Reads
PHP mt_getrandmax() Function
The mt_getrandmax() is an inbuilt function in PHP that is used to return the largest possible value of random numbers. Syntaxmt_getrandmax(): intParameter This function does not accept any parameter. Return Value The mt_getrandmax() function returns the integer representing the maximum value that ca
1 min read
PHP | gmp_random_range() Function
The gmp_random_range() is an inbuilt function in PHP which generates a random number.The random number thus generated lies between range min to max. Here GMP refers to (GNU Multiple Precision) which is for large numbers. Syntax: gmp_random_range ( GMP $min, GMP $max ) Parameters: The function accept
2 min read
Program to generate random string in PHP
Given a size N and the task is to generate a random string of size N. Examples: Input: 5Output: eR3DsInput: 10Output: MPRCyBgdcnUsing a Domain String and Random IndexCreate a domain string that contains small letters, capital letters, and the digits (0 to 9). Then generate a random number pick the c
2 min read
PHP | Imagick getImageExtrema() Function
The Imagick::getImageExtrema() function is an inbuilt function in PHP which is used to get the extrema for an image. Extrema are the points at which a maximum or minimum value of a function is observed. Syntax: array Imagick::getImageExtrema( void ) Parameters: This function doesn't accept any param
1 min read
PHP log(), log10() Functions
Logarithm is the counter operation to exponentiation. The logarithm of a number is in fact the exponent to which the other number i.e. the base must be raised to produce that number. If the Euler's Number 'e' is used as the base of any logarithmic operation it is then known as Natural Logarithmic Op
2 min read
PHP is_finite(), is_infinite(), is_nan() Functions
Given any numerical value, it can be classified into 3 different classes such as Finite Number, Infinite Number, and Not a Number or commonly known as NaN. While developing a project that is highly dependent on User Inputs there might be many cases where the user provides inappropriate inputs while
3 min read
PHP | IntlChar getIntPropertyMinValue() Function
The IntlChar::getIntPropertyMinValue() function is an inbuilt function in PHP which is used to get the minimum value for a Unicode property. This could be used to access the information as well as manipulating the Unicode characters. For an enumerated/integer/binary Unicode property, this is going t
1 min read
PHP mhash_get_hash_name() Function
The mhash_get_hash_name() function is an inbuilt function in PHP which is used to get the block size of the specified hash. It gets the highest available hash ID within the current MHash installed in the system like SHA1, MD%, etc. Syntax: string mhash_get_hash_name( int $hash ) Parameter: This func
1 min read
PHP Image Processing and GD Functions Complete Reference
Image processing and GD functions are used to create and manipulate image files in different image formats including GIF, PNG, JPEG, WBMP, and XPM. PHP can deliver the output image directly to the browser. The image processing and GD functions are used to compile the image functions for this work. I
3 min read
PHP | Imagick getQuantum() Function
The Imagick::getQuantum() function is an inbuilt function in PHP which is used to get the quantum range as an integer. Syntax: int Imagick::getQuantum( void ) Parameters: This function doesnât accepts any parameter. Return Value: This function returns an integer value containing the quantum range. E
1 min read