round($x) rounds to the nearest whole number, floor($x) always rounds down and ceil($x) always rounds up. Give round() a second argument for decimal places: round(3.14159, 2) is 3.14.
All three return a float, even when the value is whole. var_dump(round(5)) prints float(5), so cast with (int) when you need an integer.
Round to 2 decimal places
The second argument of round() is the precision. A positive number rounds to that many decimal places. A negative number rounds to the left of the decimal point, which is handy for rough figures like "about 1,200 visitors".
round(3.10, 2) prints 3.1 because a float has no idea how many zeros you wanted. When the number is going on a page (prices, percentages), format it instead: number_format() turns 3.1 into "3.10" with number_format(3.1, 2), and sprintf('%.2f', $x) does the same.
Round half up, half down or half even
By default round() rounds a half away from zero, which is the 四捨五入 most people learned in school. The third argument picks a different rule for values exactly halfway between two numbers.
PHP_ROUND_HALF_EVEN is "banker's rounding": halves go to the nearest even number, so rounding many values does not drift upward over time. Accounting code often wants it.
floor and ceil with negative numbers
floor() moves toward negative infinity and ceil() toward positive infinity. With negative numbers that surprises people: floor(-3.5) is -4, not -3. If you want to cut off the decimals (切り捨て toward zero), cast to int instead.
The pagination line is the classic use of ceil(): any leftover item still needs a page.
Integer division and modulo
/ returns a float when the division is not even, so 7 / 2 is 3.5. For whole-number division use intdiv(), which drops the remainder, and % for the remainder itself.
% converts both operands to integers first, so 7.5 % 2 is 1, and since PHP 8.1 it also prints Deprecated: Implicit conversion from float 7.5 to int loses precision. Use fmod() when the decimals matter. Dividing by zero with /, intdiv() or % throws a DivisionByZeroError in PHP 8, so check the divisor first when it comes from input.
Powers, roots, abs, max and min
** raises to a power (pow() does the same), sqrt() takes a square root and abs() removes the sign. max() and min() take either several arguments or one array.
** is evaluated right to left, so 2 ** 3 ** 2 means 2 ** 9. The max(0, min(100, $x)) pair is the usual way to clamp a value between a lower and an upper limit. M_PI holds pi if you need it for circles.
Random numbers with random_int and rand
random_int($min, $max) returns a random integer with both ends included. The output changes on every run, so press Run a few times.
Use random_int() for anything security related: login codes, tokens, password reset links. rand() and mt_rand() still work and accept the same ($min, $max) arguments, but their sequence can be predicted. For shuffling and picking with a secure source, PHP 8.2 added Random\Randomizer ($r->getInt(1, 6), $r->shuffleArray($a)).
Why floor() sometimes gives the wrong answer
Floats are stored in binary, and many decimal values (like 0.1) cannot be stored exactly. Most of the time printing hides that, but floor() and (int) do not round, they cut, and a value like 7.999999999 becomes 7.
When you need exact decimals, as with money, store whole units (cents as integers: 1999 instead of 19.99) and divide only for display. Rounding to a sensible precision before floor() or ceil(), as in the last line, also fixes most cases.
Frequently Asked Questions
How do I round to 2 decimal places in PHP?
Pass the number of decimals as the second argument: round(3.14159, 2) returns 3.14. It returns a float, so for display with a fixed number of decimals (3.10 rather than 3.1) use number_format($x, 2) or sprintf('%.2f', $x).
How do I round down in PHP?
floor($x) rounds down to the next lower whole number: floor(4.9) is 4 and floor(-4.1) is -5. To drop the decimals toward zero instead, cast with (int) $x, which turns -4.9 into -4.
Why does round() return a float in PHP?
round(), floor() and ceil() always return a float, even when the result is whole: var_dump(round(5)) prints float(5). Wrap the call in (int) or intval() when you need an integer, for example as an array index.
What is the difference between rand() and random_int() in PHP?
random_int(1, 6) uses a cryptographically secure source and is the one to use for tokens, codes and anything a user could try to guess. rand() and mt_rand() are faster but predictable. All three include both ends of the range.
How do I get the remainder of a division in PHP?
Use the % operator for integers: 17 % 5 is 2. The result takes the sign of the left operand, so -7 % 3 is -1. For floats use fmod(7.5, 2), which returns 1.5.