Why Is This Important?
- Rounding and generating random numbers are the foundation of many tasks: calculators, shopping carts, pagination, games, A/B testing.
- round, ceil, and floor help control calculation precision and how numbers are displayed.
- rand (and modern alternatives) are used to generate random numbers in PHP.
Basics: What Do These Functions Do?
- round(float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float Rounds a number to the given decimal places (precision) and using a specified rounding mode. By default: “half up” rounding.
- ceil(float $num): float Returns the smallest integer greater than or equal to the given number (rounds up).
- floor(float $num): float Returns the largest integer less than or equal to the given number (rounds down).
- rand(int $min, int $max): int Returns a random integer from the range [min, max] (both inclusive).
⚠️ Note: ceil and floor always round to integers. round lets you control decimal precision and how halves (e.g., 1.5) are treated - the mode is passed as one of PHP's built-in constants, such as PHP_ROUND_HALF_UP.
PHP Code Examples
round - rounding to precision and mode
<?php
// Basic rounding to nearest integer
echo round(3.2); // 3
echo PHP_EOL;
echo round(3.5); // 4 (default half up)
echo PHP_EOL;
// Decimal places (precision):
echo round(3.14159, 2); // 3.14
echo PHP_EOL;
echo round(3.145, 2); // 3.15 (half up)
echo PHP_EOL;
// Negative precision - tens, hundreds, etc.:
echo round(1234.567, -1); // 1230
echo PHP_EOL;
echo round(1234.567, -2); // 1200
echo PHP_EOL;
// Different rounding modes
echo round(2.5, 0, PHP_ROUND_HALF_UP); // 3
echo PHP_EOL;
echo round(2.5, 0, PHP_ROUND_HALF_DOWN); // 2
echo PHP_EOL;
echo round(2.5, 0, PHP_ROUND_HALF_EVEN); // 2 (banker’s rounding)
echo PHP_EOL;
echo round(2.5, 0, PHP_ROUND_HALF_ODD); // 3
echo PHP_EOL;
// round() always returns float
var_dump(round(10.0)); // float(10)
⚠️ Floating point caveat: numbers like 2.675 can’t be represented exactly in binary - this comes straight from the precision limits of the float data type:
<?php
echo round(2.675, 2); // often 2.67 instead of 2.68 (due to float representation)
Solutions: work in cents (integers), use BCMath/Brick\Math, or format output (number_format) instead of true rounding.
ceil - rounding up
<?php
echo ceil(3.01); // 4
echo PHP_EOL;
echo ceil(3.99); // 4
echo PHP_EOL;
echo ceil(3.0); // 3
echo PHP_EOL;
// Negative numbers:
echo ceil(-3.1); // -3 (towards zero)
echo PHP_EOL;
echo ceil(-3.9); // -3
floor - rounding down
<?php
echo floor(3.99); // 3
echo PHP_EOL;
echo floor(3.01); // 3
echo PHP_EOL;
// Negative numbers:
echo floor(-3.1); // -4 (away from zero)
echo PHP_EOL;
echo floor(-3.9); // -4
floor/ceil vs casting to int
<?php
echo (int) 3.9; // 3 (truncates towards zero)
echo PHP_EOL;
echo (int) -3.9; // -3 (differs from floor(-3.9) == -4)
Trick: ceil/floor to decimal places
Since ceil and floor don’t have precision parameter, scale the number using the arithmetic operators from the earlier lesson:
<?php
$val = 12.345;
echo ceil($val * 100) / 100; // 12.35
echo PHP_EOL;
echo floor($val * 100) / 100; // 12.34
number_format vs round - formatting output
For nice display of prices/numbers, prefer formatting:
<?php
$price = 1234.5;
echo number_format($price, 2, ',', ' '); // "1 234,50"
number_format doesn’t change the numeric value, only returns a string for display.
rand - random numbers
<?php
// Dice roll: 1-6
echo rand(1, 6);
echo PHP_EOL;
// Random index
$colors = ['red', 'green', 'blue'];
$randomIndex = rand(0, count($colors) - 1);
echo $colors[$randomIndex];
echo PHP_EOL;
// Random float [0,1)
$float = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
echo $float;
Modern PHP (7.1+) implements rand using Mersenne Twister, but it’s not cryptographically secure.
For security (tokens, passwords, keys), use:
<?php
$safe = random_int(1, 100);
$bytes = random_bytes(16);
$token = bin2hex($bytes);
echo $token;
Best Practices and Common Mistakes
Best Practices
- For finance, avoid floats: use integers (cents) or BCMath/Brick\Math.
- For display, prefer number_format over round.
- Pick rounding mode intentionally (e.g., banker’s rounding in accounting).
- Be careful with negatives in ceil/floor (up/down are relative to number line).
- For secure randomness: use random_int and random_bytes, not rand.
- Remember rand’s inclusive range [min, max].
Common Mistakes
- Expecting round(2.675, 2) to give 2.68 - float limitation.
- Confusing casting (int)$x with floor/ceil for negatives.
- Using rand for cryptography (insecure).
- Rounding multiple times unnecessarily instead of once at output.
- Assuming ceil/floor/round return int - in PHP, they return float. Cast explicitly if needed.
Summary
- round: flexible rounding with precision and mode (HALF_UP, HALF_DOWN, HALF_EVEN, HALF_ODD).
- ceil: up to next integer; for negatives, towards zero.
- floor: down to previous integer; for negatives, away from zero.
- rand: simple random ints (not secure). Use random_int/random_bytes for secure randomness.
- Use number_format for display, and int/BCMath for financial calculations.
Mini Quiz
- What does round(2.5) return? → 3
- What does round(2.5, 0, PHP_ROUND_HALF_EVEN) return? → 2
- What does ceil(-3.2) return? → -3
- What does floor(-3.2) return? → -4
- Difference: (int)-4.9 vs floor(-4.9)? → -3 vs -5
- round(1234.567, -2)? → 1200
- Can rand(1,10) return 10? → Yes
- Secure random integer? → random_int
- Cut to 2 decimals without rounding up? → floor($x * 100)/100
- Best way to display formatted price? → number_format($price, 2, ',', ' ')
Now you know how to use the essential math functions in PHP safely and effectively. This wraps up the basics chapter - the rest of the free PHP course builds on everything you have learned here.