In this tutorial I will present PHP math functions. They may be needed by a developer in small or complex web applications that are dealing with numbers. I will show you abs(), base_convert(), floor(), fmod(), is_infinite() and others.
abs(nr) – returns the absolute value (with +) of the numbers passed as a parameter to the function.
After calling this code the results would be: 2.8 (double / float), 3 (integer) and 1 (integer).
base_convert(nr, base1, base2) – converts a nr. from base1 into base2.
The result would be 10010 (base 2) which represents the value 18 in base 10.
ceil(nr) – returns the rounded value of nr to the next bigger integer.
The results will be : 4, 4, -3, and 2 .
floor(nr) – is the opposite of ceil.
The results will be: 3, 4, -3, 1.
fmod(x, y) – returns the remainder of x divided to y.
$r will have the value 1.5, because $x divided to $y is 1, remainder is 1.5 .
is_infinite(nr) – returns TRUE if nr is infinite number, FALSE otherwise.
lcg_value() – returns a random number between 0 and 1.
max(nr1, n2, nr3, ….nr_n) – returns the highest number between nr2 and nr_n. min() does the opposite of max.
The result will be 100.
mt_rand(min, max) returns a random number between min and max. Same like rand() but mt_rand is four times faster.
pow(nr, power) – returns the value of nr at the power of “power”.
The result is 8.
sqrt(nr) – extracts the square root from nr.
The result will be 15.
is_numeric(val) – returns TRUE if val is numeric value, FALSE otherwise.
Also we have cos, sin, tan, which are taking as a parameter the nr. of degrees (30, 60, 90 etc.) and returns the value in radians.
Leave a Reply