PHP Math:
Math functions and constants are already defined in the PHP library which can be used to perform mathematical calculations.
PHP Math Functions:
PHP provides various math functions including;
abs() function:
This function is used to get an absolute value of a number.
Syntax:
number abs ( mixed $number ) |
ceil() function:
This function is used to get a round up value.
Syntax:
float ceil ( float $value ) |
floor() function:
This function is used to get a round down value.
Syntax:
float floor ( float $value ) |
sqrt() function:
This function is used to get the square root of a number.
Syntax:
float sqrt ( float $arg ) |
decbin() function:
This function is used to convert a decimal number into binary.
Syntax:
string decbin ( int $number ) |
dechex() function:
This function is used to convert a decimal number into hexadecimal.
Syntax:
string dechex ( int $number ) |
decoct() function:
This function is used to convert a decimal number into octal.
Syntax:
string decoct ( int $number ) |
base_convert() function:
This function is used to convert a base number to any other base number (hexadecimal number to binary, binary to octal, etc.)
Syntax:
string base_convert ( string $number , int $frombase , int $tobase ) |
bindec() function:
This function is used to convert a binary number into decimal.
Syntax:
number bindec ( string $binary_string ) |
There are numerous other functions defined by PHP which are used to perform mathematical operations. Some of the MOST USED string functions are listed below:
FUNCTION | USES |
abs() | It is used to find the absolute (positive) value of a number. |
sin() | It is used to return the sine of a number. |
cos() | It is used to find the cosine of a number. |
tan() | It is used to return the 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 |
min() | It returns the lowest value in an array, or the lowest value of several specified values. |
max() | It is used to return the highest value in an array, or the highest value of several specified values. |
pow() | It is used to return x raised to the power of y. |
rand() | It is used to generates a random integer. |
round() | It is used to round a floating-point number. |
sqrt() | It is used to return the square root of a number. |
PHP Math Constants:
PHP provides various math constants, for example;
- INF: INF represents infinite and the value of this constant is infinite.
- M_E: M_E is used to return e and the value of this constant is 2.7182818284590452354.
- M_EULER: M_EULER is used to return Euler constant and the value of this constant is 0.57721566490153286061.
Example
<!DOCTYPE html> <html> <body> <?php echo "absolute value<br/>"; echo (abs(-7)."<br/>"); echo "round up value<br/>"; echo (ceil(7.333)."<br/>"); echo "round down value<br/>"; echo (floor(7.333)."<br/>"); echo "square root<br/>"; echo (sqrt(25)."<br/>"); echo "decimal to binary<br/>"; echo (decbin(2)."<br/>"); echo "decimal to hex<br/>"; echo (dechex(10)."<br/>"); echo "decimal to octal<br/>"; echo (decoct(10)."<br/>"); echo "base conversion<br/>"; $n1=10; echo (base_convert($n1,10,2)."<br/>"); echo "binary to decimal<br/>"; echo (bindec(1010)."<br/>"); ?> </body> </html> |
Output
absolute value 7 round up value 8 round down value 7 square root 5 decimal to binary 10 decimal to hex a decimal to octal 12 base conversion 1010 binary to decimal 10 |