PHP Math
Menu
Here are some examples of using the most commonly used math functions in PHP:
- abs(): Returns the absolute value of a number.
.
$number = -10;
$absValue = abs($number); // returns 10
- sqrt(): Returns the square root of a number.
$number = 25;
$sqrtValue = sqrt($number); // returns 5
- pow(): Returns the result of raising a number to a specified power.
$number = 2;
$power = 3;
$result = pow($number, $power); // returns 8 (2 raised to the power of 3)
- exp(): Returns the exponential value of a number.
$number = 2;
$expValue = exp($number); // returns approximately 7.389 (e raised to the power of 2)
- log(): Returns the natural logarithm of a number.
$number = 10;
$logValue = log($number); // returns approximately 2.303 (natural logarithm of 10)
- log10(): Returns the base-10 logarithm of a number.
$number = 100;
$log10Value = log10($number); // returns 2 (base-10 logarithm of 100)
- ceil(): Rounds a number up to the nearest integer.
$number = 3.14;
$ceilValue = ceil($number); // returns 4
- floor(): Rounds a number down to the nearest integer.
$number = 3.14;
$floorValue = floor($number); // returns 3
- round(): Rounds a number to the nearest integer.
$number = 3.5;
$roundValue = round($number); // returns 4
These are just a few examples of the many math functions available in PHP. By using these functions, developers can perform a wide range of mathematical operations and calculations within their PHP applications.
Menu
Join To Get Our Newsletter