Step 1 of 5 · Reading · ~4 min
Learn
Getting Started
Numbers and Math
PHP has exactly two numeric types: int for whole numbers and float for everything with a decimal point. You never declare which one you want — the operator and the operands decide.
The operators
<?php
$a = 17;
$b = 5;
echo $a + $b, "\n";
echo $a - $b, "\n";
echo $a * $b, "\n";
echo $a / $b, "\n";
echo intdiv($a, $b), "\n";
echo $a % $b, "\n";
echo $a ** $b, "\n";
//> 22
//> 12
//> 85
//> 3.4
//> 3
//> 2
//> 1419857
Why did division give me a decimal?
Because / is true division. It hands back an int only when the division comes out exact, and a float otherwise.
| Expression | Result | Type |
|---|---|---|
8 / 2 | 4 | int — it divided evenly |
7 / 2 | 3.5 | float |
17 / 5 | 3.4 | float |
intdiv(17, 5) | 3 | int — always |
17 % 5 | 2 | int — the remainder |
If you want the whole-number part, say so with intdiv(). If you want the leftover, use %. Coming from Python, note that PHP's / behaves like Python's /, and intdiv() fills in for Python's // — there is no // operator here.
What about negative numbers?
intdiv() chops toward zero. floor() chops toward negative infinity. They agree on positive numbers and disagree on negatives, which is a classic source of off-by-one bugs.
<?php
echo intdiv(-7, 2), "\n"; // toward zero
echo floor(-7 / 2), "\n"; // toward minus infinity
echo -7 % 3, "\n"; // the sign follows the LEFT operand
echo 7 % -3, "\n";
//> -3
//> -4
//> -1
//> 1
Integers do not wrap around
Overflow a PHP integer and it quietly becomes a float, instead of wrapping around to a negative number the way C would.
<?php
echo PHP_INT_MAX, "\n";
var_dump(PHP_INT_MAX + 1);
//> 9223372036854775807
//> float(9.2233720368548E+18)
You gain range and lose exactness: a float carries roughly 15–16 significant digits, and var_dump on this platform prints about 14 of them.
Floats are approximations
0.1 cannot be written exactly in binary, so neither can 0.1 + 0.2.
| ✗ Do not | ✓ Do |
|---|---|
if ($a === $b) on two floats | if (abs($a - $b) < 1e-9) |
trust that 0.1 + 0.2 === 0.3 | it is false |
Compare floats by asking whether the gap between them is small enough: . This is not a PHP quirk — it is how binary floating point works in every language.
The number toolbox
| Function | Does |
|---|---|
abs($n) | magnitude, sign removed |
floor($n) / ceil($n) | round down / up |
round($n, $places) | round, half away from zero |
min(...) / max(...) | smallest / largest, of arguments or of one array |
sqrt($n) / pow($a, $b) | root / power (** is the same as pow) |
intval($s) / floatval($s) | parse a string as a number |
number_format($n, 2) | turn 1234567.891 into readable output |
<?php
echo floor(2.7), " ", ceil(2.1), " ", round(2.5), " ", round(2.4567, 2), "\n";
echo number_format(1234567.891, 2), "\n";
//> 2 3 3 2.46
//> 1,234,567.89
Your exercise
Rectangle Area reads a width and a height, one per line, and prints their product.
The starter reads both values and casts them to int, so the answer is a single echo. The mistake the grader catches is decoration: Area: 15 and 15 sq units both fail against the expected 15. (Whitespace at the very end of your output is trimmed before the comparison, so a forgotten final line break is forgiven here — write it anyway, because a break in the middle of a multi-line answer is not.) Use * — not +, and definitely not /, which would drag a decimal point into your output. Multiplying two ints stays an int. The hidden case is a 1 by 999 rectangle, so nothing that quietly assumes a square will survive it.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…