PHP Math
PHP provides many predefined math constants and functions that can be used to perform mathematical operations.
PHP Math: abs() function
The abs() function returns absolute value of given number. It returns an integer value but if you pass floating point value, it returns a float value.
Syntax
number abs ( mixed $number )
Example
"); // 7 (integer)
echo (abs(7)." "); //7 (integer)
echo (abs(-7.2)." "); //7.2 (float/double)
?>
Output:
PHP Math: ceil() function
The ceil() function rounds fractions up.
Syntax
float ceil ( float $value )
Example
");// 4
echo (ceil(7.333)." ");// 8
echo (ceil(-4.8)." ");// -4
?>
Output:
PHP Math: floor() function
The floor() function rounds fractions down.
Syntax
float floor ( float $value )
Example
");// 3
echo (floor(7.333)." ");// 7
echo (floor(-4.8)." ");// -5
?>
Output:
PHP Math: sqrt() function
The sqrt() function returns square root of given argument.
Syntax
float sqrt ( float $arg )
Example
");// 4
echo (sqrt(25)." ");// 5
echo (sqrt(7)." ");// 2.6457513110646
?>
Output:
PHP Math: decbin() function
The decbin() function converts decimal number into binary. It returns binary number as a string.
Syntax
string decbin ( int $number )
Example
");// 10
echo (decbin(10)." ");// 1010
echo (decbin(22)." ");// 10110
?>
Output:
PHP Math: dechex() function
The dechex() function converts decimal number into hexadecimal. It returns hexadecimal representation of given number as a string.
Syntax
string dechex ( int $number )
Example
");// 2
echo (dechex(10)." ");// a
echo (dechex(22)." ");// 16
?>
Output:
PHP Math: decoct() function
The decoct() function converts decimal number into octal. It returns octal representation of given number as a string.
Syntax
string decoct ( int $number )
Example
");// 2
echo (decoct(10)." ");// 12
echo (decoct(22)." ");// 26
?>
Output:
PHP Math: base_convert() function
The base_convert() function allows you to convert any base number to any base number. For example, you can convert hexadecimal number to binary, hexadecimal to octal, binary to octal, octal to hexadecimal, binary to decimal etc.
Syntax
string base_convert ( string $number , int $frombase , int $tobase )
Example
Output:
PHP Math: bindec() function
The bindec() function converts binary number into decimal.
Syntax
number bindec ( string $binary_string )
Example
");// 2
echo (bindec(1010)." ");// 10
echo (bindec(1011)." ");// 11
?>
Output:
PHP Math Functions
Let's see the list of important PHP math functions.
Function |
Description |
abs() |
It is used to find the absolute (positive) value of a number. |
sin() |
It is used to return the sine of a number. |
sinh() |
It is used to return the hyperbolic sine of a number. |
asin() |
It is used to find the arc sine of a number. |
asinh() |
It is used to find the inverse hyperbolic sine of a number. |
cos() |
It is used to find the cosine of a number. |
cosh() |
It is used to return the cosh of a number. |
acos() |
It is used to return the arc cosine of a number. |
acosh() |
It is used to return the inverse hyperbolic cosine of a number. |
tan() |
It is used to return the tangent of a number. |
tanh() |
It is used to return the hyperbolic tangent of a number. |
atan() |
It is used to return the arc tangent of a number in radians. |
atan2() |
It is used to return the arc tangent of two variables x and y. |
atanh() |
It is used to return the inverse hyperbolic tangent of a number. |
base_convert() |
It is used to convert a number from one number base to another. |
bindec() |
It is used to convert a binary number to a decimal number. |
ceil() |
It is used to find round a number up to the nearest integer. |
pi() |
It returns the approximation value of PI. |
decbin() |
It converts a decimal number to a binary number. |
dechex() |
It converts a decimal number to a hexadecimal number. |
decoct() |
It converts a decimal number to an octal number |
deg2rad() |
It converts a degree value to a radian value. |
rad2deg() |
It converts a radian value to a degree value. |
exp() |
It is used to calculate the exponent of e. |
expm1() |
It is used to return exp(x) - 1. |
floor() |
It converts round a number down to the nearest integer. |
fmod() |
It returns the remainder of x/y. |
getrandmax() |
It returns the largest possible value returned by rand(). |
hexadec() |
It is used to convert a hexadecimal number to a decimal number. |
hypot() |
It is used to calculate the hypotenuse of a right-angle triangle. |
is_finite() |
To check whether a value is finite or not. |
is_infinite() |
It is used to check whether a value is infinite or not. |
is_nan() |
It is used to check whether a value is 'not-a-number'. |
lcg_value() |
It is used to return a pseudo random number in a range between 0 and 1. |
log() |
It is used to return the natural logarithm of a number. |
log10() |
It is used to return the base-10 logarithm of a number. |
log1p() |
It is used to return log(1+number). |
max() |
It is used to return the highest value in an array, or the highest value of several specified values. |
min() |
It returns the lowest value in an array, or the lowest value of several specified values. |
getrandmax() |
It is used to return the maximum value by using rand(). |
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() |
It is used to converts an octal number to a decimal number. |
pow() |
It is used to return x raised to the power of y. |
intdiv |
It returns the integer quotient of the division of dividend by divisor. |
rand() |
It is used to generates a random integer. |
round() |
It is used to round a floating-point number. |
fmod() |
It is used to return the floating point remainder of the division of the argument. |
sqrt() |
It is used to return the square root of a number. |
|