PHP Math Functions
PHP includes a comprehensive set of built-in math functions that handle everything from rounding and absolute values to generating random numbers and calculating square roots. These functions are available without any imports or includes — they work out of the box in any PHP script.
Basic Math Functions
abs() — Absolute Value
The abs() function returns the absolute (positive) value of a number, removing any negative sign.
<?php
echo abs(-25); // Outputs: 25
echo abs(25); // Outputs: 25
echo abs(-3.7); // Outputs: 3.7
?>
pow() — Exponentiation
The pow() function raises a number to the power of another number. PHP 7.4+ also supports the ** operator as a shorthand.
<?php
echo pow(2, 8); // Outputs: 256 (2 to the 8th power)
echo pow(3, 3); // Outputs: 27 (3 cubed)
echo 2 ** 10; // Outputs: 1024 (shorthand operator)
?>
sqrt() — Square Root
<?php
echo sqrt(144); // Outputs: 12
echo sqrt(2); // Outputs: 1.4142135623731
?>
Rounding Functions
PHP provides three functions for rounding numbers, each with different behavior.
round() — Standard Rounding
Rounds a float to the nearest integer. An optional second argument specifies the number of decimal places to keep.
<?php
echo round(4.3); // Outputs: 4 (rounds down)
echo round(4.5); // Outputs: 5 (rounds up)
echo round(4.7); // Outputs: 5 (rounds up)
echo round(2.556, 2); // Outputs: 2.56 (rounded to 2 decimal places)
echo round(-3.5); // Outputs: -4 (rounds away from zero)
?>
ceil() — Round Up
Always rounds a number UP to the nearest integer, regardless of the decimal value.
<?php
echo ceil(4.1); // Outputs: 5
echo ceil(4.9); // Outputs: 5
echo ceil(-4.5); // Outputs: -4 (closer to zero when rounding up)
?>
A practical use: calculating the number of pages needed to display results. If there are 23 results and 5 per page, ceil(23 / 5) returns 5 pages.
floor() — Round Down
Always rounds a number DOWN to the nearest integer.
<?php
echo floor(4.9); // Outputs: 4
echo floor(4.1); // Outputs: 4
echo floor(-4.5); // Outputs: -5 (away from zero when rounding down)
?>
Finding Minimum and Maximum
<?php
echo min(3, 7, 1, 9, 2); // Outputs: 1
echo max(3, 7, 1, 9, 2); // Outputs: 9
// Works with arrays too
$scores = [88, 95, 72, 100, 63];
echo min($scores); // Outputs: 63
echo max($scores); // Outputs: 100
?>
Random Number Generation
rand() — Random Integer
The rand() function generates a random integer. Provide minimum and maximum values to limit the range.
<?php
echo rand(); // Random integer between 0 and PHP_INT_MAX
echo rand(1, 10); // Random integer between 1 and 10 (inclusive)
echo rand(100, 999); // Random three-digit number
?>
mt_rand() — Faster Random Integer
The mt_rand() function uses the Mersenne Twister algorithm and is significantly faster and more statistically random than rand(). It is preferred for most use cases.
<?php
echo mt_rand(1, 100); // Random integer from 1 to 100
?>
Simulating a Dice Roll
<?php
$diceRoll = rand(1, 6);
echo "You rolled: " . $diceRoll;
?>
Number Formatting
number_format() — Formatting Numbers for Display
The number_format() function formats a number with grouped thousands and a specified number of decimal places.
<?php
$amount = 1234567.891;
echo number_format($amount); // Outputs: 1,234,568 (no decimals)
echo number_format($amount, 2); // Outputs: 1,234,567.89
echo number_format($amount, 2, '.', ','); // Outputs: 1,234,567.89
echo number_format($amount, 2, ',', '.'); // Outputs: 1.234.567,89 (European format)
?>
fmod() — Floating-Point Modulus
<?php
echo fmod(10.5, 3.2); // Outputs: 0.9 (remainder when 10.5 is divided by 3.2)
?>
Logarithm and Pi
<?php
echo log(M_E); // Natural log of e = 1
echo log(100, 10); // Log base 10 of 100 = 2
echo M_PI; // Outputs: 3.1415926535898 (built-in constant)
echo M_E; // Outputs: 2.718281828459 (Euler's number)
?>
Checking for Finite and Infinite Values
<?php
$result = 10 / 0.0000001; // Very large number
var_dump(is_finite($result)); // bool(true) if within float range
var_dump(is_infinite($result)); // bool(false)
var_dump(is_nan("hello")); // bool(false) - not a number check
?>
Practical Example — Calculating a Discount
<?php
$originalPrice = 250.00;
$discountPercent = 15;
$discountAmount = ($originalPrice * $discountPercent) / 100;
$finalPrice = $originalPrice - $discountAmount;
echo "Original Price: $" . number_format($originalPrice, 2) . "\n";
echo "Discount: $" . number_format($discountAmount, 2) . "\n";
echo "Final Price: $" . number_format($finalPrice, 2);
// Outputs:
// Original Price: $250.00
// Discount: $37.50
// Final Price: $212.50
?>
Key Points
abs()returns the absolute (positive) value of a number.round()rounds normally;ceil()always rounds up;floor()always rounds down.sqrt()calculates the square root;pow()or**handles exponentiation.min()andmax()find the smallest and largest values from a list or array.rand(min, max)generates a random integer in the given range.number_format()formats numbers for human-readable display with commas and decimals.- PHP provides built-in constants like
M_PIandM_Efor mathematical constants.
