PHP provides an arsenal of mathematical tools that serve as the bedrock of countless dynamic web applications. Harnessing their capabilities empowers you to conquer complex numeric challenges, making these functions vital to any developer's toolkit.
Whether you're a seasoned PHP developer or just embarking on your coding journey, this guide provides a comprehensive list of PHP Math Functions that you’ll want to keep handy.
From fundamental operations such as absolute value and rounding, to trigonometric and logarithmic calculations, these functions streamline tasks, eliminate complexity, and enhance the efficiency of your code.
Keep reading for the full list of functions, or use this table to jump to the exact one that you're looking for.
PHP Math Functions List
1. Absolute Value: abs()
This function returns the absolute value of a number.
Example:
<?php
$number = -5;
$absoluteValue = abs($number);
echo "Absolute Value: $absoluteValue";
?>
Output:
Absolute Value: 5
2. Maximum: max()
This function returns the highest value from a list of numbers.
Example:
<?php
$numbers = [12, 7, 25, 8, 14];
$maximum = max($numbers);
echo "Maximum Value: $maximum";
?>
Output:
Maximum Value: 25
3. Minimum: min()
This function returns the lowest value from a list of numbers.
Example:
<?php
$numbers = [12, 7, 25, 8, 14];
$minimum = min($numbers);
echo "Minimum Value: $minimum";
?>
Output:
Minimum Value: 7
4. Round-Up: ceil()
This function rounds a number up to the nearest integer.
Example:
<?php
$number = 6.25;
$roundedUp = ceil($number);
echo "Rounded Up: $roundedUp";
?>
Output:
Rounded Up: 7
5. Round Down: floor()
This function rounds a number down to the nearest integer.
Example:
<?php
$number = 6.75;
$roundedDown = floor($number);
echo "Rounded Down: $roundedDown";
?>
Output:
Rounded Down: 6
6. Cosine: cos()
This function returns the cosine of an angle in radians.
Example:
<?php
$angle = pi() / 3; // 60 degrees in radians
$cosineValue = cos($angle);
echo "Cosine Value: $cosineValue";
?>
Output:
Cosine Value: 0.5
7. Hyperbolic Cosine: cosh()
This function returns the hyperbolic cosine of a number.
Example:
<?php
$number = 2;
$hyperbolicCosine = cosh($number);
echo "Hyperbolic Cosine: $hyperbolicCosine";
?>
Output:
Hyperbolic Cosine: 3.7621956910836
8. Arc Cosine: acos()
This function returns the arc cosine of a number.
Example:
<?php
$value = 0.5;
$arcCosine = acos($value);
echo "Arc Cosine: $arcCosine";
?>
Output:
Arc Cosine: 1.0471975511966
9. Inverse Hyperbolic Cosie: acosh()
This function returns the inverse hyperbolic cosine of a number.
Example:
<?php
$number = 3.7621956910836;
$inverseHyperbolicCosine = acosh($number);
echo "Inverse Hyperbolic Cosine: $inverseHyperbolicCosine";
?>
Output:
Inverse Hyperbolic Cosine: 2
10. Sine: sin()
This function returns the sine of an angle in radians.
Example:
<?php
$angle = pi() / 6; // 30 degrees in radians
$sineValue = sin($angle);
echo "Sine Value: $sineValue";
?>
Output:
Sine Value: 0.5
11. Hyperbolic Sine: sinh()
This function returns the hyperbolic sine of a number.
Example:
<?php
$number = 2;
$hyperbolicSine = sinh($number);
echo "Hyperbolic Sine: $hyperbolicSine";
?>
Output:
Hyperbolic Sine: 3.626860407847
12. Arc Sine: asin()
This function returns the arc sine of a number.
Example:
<?php
$value = 0.5;
$arcSine = asin($value);
echo "Arc Sine: $arcSine";
?>
Output:
Arc Sine: 0.5235987755983
13. Inverse Hyperbolic Sine: asinh()
This function returns the arc sine of a number.
Example:
<?php
$value = 0.5;
$arcSine = asin($value);
echo "Arc Sine: $arcSine";
?>
Output:
Arc Sine: 0.5235987755983
14. Tangent: tan()
This function returns the tangent of an angle in radians.
Example:
<?php
$angle = pi() / 4; // 45 degrees in radians
$tangentValue = tan($angle);
echo "Tangent Value: $tangentValue";
?>
Output:
Tangent Value: 1
15. Hyperbolic Tangent: tanh()
This function returns the hyperbolic tangent of a number.
Example:
<?php
$number = 1;
$hyperbolicTangent = tanh($number);
echo "Hyperbolic Tangent: $hyperbolicTangent";
?>
Output:
Hyperbolic Tangent: 0.76159415595576
16. Arc Tangent: atan()
This function returns the arc tangent of a number.
Example:
<?php
$value = 1;
$arcTangent = atan($value);
echo "Arc Tangent: $arcTangent";
?>
Output:
Arc Tangent: 0.78539816339745
17. Two Variable Arc Tangent: atan2()
This function returns the arc tangent of two variables.
Example:
<?php
$y = 1;
$x = 1;
$arcTangent = atan2($y, $x);
echo "Arc Tangent: $arcTangent";
?>
Output:
Arc Tangent: 0.78539816339745
18. Inverse Hyperbolic Tangent: atanh()
This function returns the inverse hyperbolic tangent of a number.
Example:
<?php
$number = 0.76159415595576;
$inverseHyperbolicTangent = atanh($number);
echo "Inverse Hyperbolic Tangent: $inverseHyperbolicTangent";
?>
Output:
Inverse Hyperbolic Tangent: 0.99999999999999
19. Degree to Radians: deg2rad()
This function converts degrees to radians.
Example:
<?php
$degrees = 90;
$radians = deg2rad($degrees);
echo "Radians: $radians";
?>
Output:
Radians: 1.5707963267949
20. Random Numer: rand()
This function generates a random number.
Example:
<?php
$randomNumber = rand(1, 100);
echo "Random Number: $randomNumber";
?>
Output:
Random Number: 12
21. Integer Division: intdiv()
This function performs integer division.
Example:
<?php
$numerator = 10;
$denominator = 3;
$result = intdiv($numerator, $denominator);
echo "Integer Division Result: $result";
?>
Output:
Integer Division Result: 3
22. Is Finite: is_finite()
This function checks if a value is a finite number.
Example:
<?php
$value = 5;
$isFinite = is_finite($value);
echo "Is Finite: " . ($isFinite ? "Yes" : "No");
?>
Output:
Is Finite: Yes
23. Is Infinite: is_infinite()
This function checks if a value is positive or negative infinity.
Example:
<?php
$value = INF; // Positive infinity
$isInfinite = is_infinite($value);
echo "Is Infinite: " . ($isInfinite ? "Yes" : "No");
?>
Output:
Integer Division Result: 3
24. Not a Number: is_nan()
This function checks if a value is NaN (Not-a-Number).
Example:
<?php
$value = acos(1.5); // acos of a value greater than 1 is NaN
$isNaN = is_nan($value);
echo "Is NaN: " . ($isNaN ? "Yes" : "No");
?>
Output:
Is NaN: Yes
25. Power: pow()
This function returns a number raised to a power.
Example:
<?php
$base = 2;
$exponent = 3;
$result = pow($base, $exponent);
echo "2^3: $result";
?>
Output:
2^3: 8
26. Natural Logarithm: log()
This function returns the natural logarithm of a number.
Example:
<?php
$number = 10;
$logarithm = log($number);
echo "Natural Logarithm: $logarithm";
?>
Output:
Natural Logarithm: 2.302585092994
27. Base-10 Logarithm: log10()
This function returns the base-10 logarithm of a number.
Example:
<?php
$number = 100;
$logarithm = log10($number);
echo "Base-10 Logarithm: $logarithm";
?>
Output:
Base-10 Logarithm: 2
28. Natural Logarithm + 1: log1p()
This function returns the natural logarithm of 1 plus a number.
Example:
<?php
$number = 1;
$logarithm = log1p($number);
echo "Natural Logarithm of (1 + 1): $logarithm";
?>
Output:
Natural Logarithm of (1 + 1): 0.69314718055995
29. Base Convert: base_convert()
This function converts a number between different number bases.
Example:
<?php
$number = "1010";
$fromBase = 2;
$toBase = 10;
$converted = base_convert($number, $fromBase, $toBase);
echo "Converted: $converted";
?>
Output:
Converted: 10
30. e Raised to a Power: exp()
This function returns the value of e raised to a power.
Example:
<?php
$exponent = 1;
$result = exp($exponent);
echo "e^1: $result";
?>
Output:
e^1: 2.718281828459
31. e Raised to a Power -1: expm1()
This function returns e raised to a power minus one.
Example:
<?php
$exponent = 1;
$result = exp($exponent);
echo "e^1: $result";
?>
Output:
e^1: 2.718281828459
32. Radian to Degrees: rad2deg()
This function converts radians to degrees.
Example:
<?php
$radians = 1.5707963267949; // Pi/2 radians
$degrees = rad2deg($radians);
echo "Degrees: $degrees";
?>
Output:
Degrees: 90
33. Decimal to Binary: decbin()
This function converts decimals to binary values.
Example:
<?php
$decimalNumber = 10;
$binaryNumber = decbin($decimalNumber);
echo "Binary: $binaryNumber";
?>
Output:
Binary: 1010
34. Binary to Decimal: bindec()
This function converts binary values to decimals.
Example:
<?php
$binaryNumber = "1010";
$decimalNumber = bindec($binaryNumber);
echo "Decimal: $decimalNumber";
?>
Output:
Decimal: 10
35. Decimal to Hexadecimal: dechex()
This function converts a decimal number to hexadecimal.
Example:
<?php
$decimalNumber = 255;
$hexadecimalNumber = dechex($decimalNumber);
echo "Hexadecimal: $hexadecimalNumber";
?>
Output:
Hexadecimal: ff
36. Decimal to Octal: decoct()
This function converts a decimal number to an octal.
Example:
<?php
$decimalNumber = 64;
$octalNumber = decoct($decimalNumber);
echo "Octal: $octalNumber";
?>
Output:
Octal: 100
37. Largest Random Value: getrandmax()
This function returns the largest possible random value.
Example:
<?php
$maxRandomValue = getrandmax();
echo "Max Random Value: $maxRandomValue";
?>
Output:
Max Random Value: 2147483647
38. Random Number Generator: mt_rand()
This function generates a random number using the Mersenne Twister algorithm. It allows you to generate random integers within a specified range.
Example:
<?php
$min = 10;
$max = 50;
$randomNumber = mt_rand($min, $max);
echo "Random Number between $min and $max: $randomNumber";
?>
Output:
Random Number between 10 and 50: 42
39. Maximum Random Value: mt_getrandmax()
This function returns the maximum random value from the mt_rand() function.
Example:
<?php
$maxRandomValue = getrandmax();
echo "Max Random Value: $maxRandomValue";
?>
Output:
Max Random Value: 2147483647
40. Hypotenuse: hypot()
This function calculates the length of the hypotenuse of a right-angle triangle.
Example:
<?php
$side1 = 3;
$side2 = 4;
$hypotenuse = hypot($side1, $side2);
echo "Hypotenuse: $hypotenuse";
?>
Output:
Hypotenuse: 5
41. Pi Function: pi()
This function returns the value of pi (π).
Example:
<?php
$piValue = pi();
echo "Value of Pi: $piValue";
?>
Output:
Value of Pi: 3.1415926535898
42. Square Root: sqrt()
This function returns the square root of a number.
Example:
<?php
$number = 25;
$squareRoot = sqrt($number);
echo "Square Root: $squareRoot";
?>
Output:
Square Root: 5
43. Remainder: fmod()
This function returns the remainder of dividing two numbers.
Example:
<?php
$dividend = 10;
$divisor = 3;
$remainder = fmod($dividend, $divisor);
echo "Remainder: $remainder";
?>
Output:
Remainder: 1
44. Hexadecimal Value: hexdec()
This function converts a hexadecimal number to its decimal equivalent.
Example:
<?php
$hexadecimalNumber = "1A"; // Hexadecimal for 26
$decimalNumber = hexdec($hexadecimalNumber);
echo "Decimal: $decimalNumber";
?>
Output:
Decimal: 26
Performing Calculations With PHP Math Functions
From foundational operations to complex trigonometric feats, these functions empower you to conquer numeric challenges with confidence and finesse. Whether your projects involve financial analysis, scientific simulations, or data processing, keep this list handy so you can reference these functions as needed over time.