Arithmetic and Assignment Operators

1.Arithmetic and Assignment Operators

M

In this chapter

We'll learn what an operator is, PHP's arithmetic operators — including the remainder and power operators — how to turn minutes into a time like 10:15, the shortcut assignment operators like += and .=, and what ++ and -- really do.

13–15 min

The Problem in Real Life

Friday, 3 p.m. Two customers walk into Tom's shop at the same time — Sam and Alex — each holding a confirmation for the 3:00 slot. Tom squeezes one in and apologises to the other. Olivia calls Mia that evening: "How did the system give one slot to two people?"

Marcus finds the line in minutes: if ($isFree && $isMember || $paysOnline). Mia stares at it. She knows $isFree — but &&, ||? "You'll fix this line yourself," says Marcus. "But first you need to read every symbol in it. Those symbols are called operators, and PHP has a lot of them."

He starts with a smaller bug in the same file. A haircut plus beard trim takes 45 minutes, and the page says the 9:30 appointment ends at 10.25:00.

M

9:30 plus 45 minutes is 10:15. Where does "10.25" come from?

Mia

Maths on Paper vs. Maths in PHP

Minutes into times

Dividing minutes by 60 gives 10.25 hours, not 10:15. You need a whole part and a remainder.

Long, repeated lines

$total = $total + $price; repeats the variable name. PHP has a shorter way.

Symbols you can't read yet

&&, ||, ??, % and the power operator all appear in real code. Each is an operator with its own rule.

Arithmetic and Assignment Operators

Operators and operands: an operator is a symbol that does something with values, like + (add) or = (assign). The values it works on are its operands: in 25 + 10, the operator is + and the operands are 25 and 10. An operator and its operands together form an expression (Act 02, chapter 2) — they produce a new value. Most operators take two operands; a few, like ! (not), take one. This Act goes through PHP's operators group by group. First, the ones for maths:

  • Plus and minus: + adds, - subtracts. A - in front of one value makes it negative: -$price.
  • Multiply and divide: * multiplies, / divides. Remember from Act 04: / gives a float unless the result is whole.
  • Remainder: % (called modulo) gives what's left over after dividing whole numbers: 17 % 5 is 2.
  • Power: ** raises to a power: 2 ** 10 is 1024.

The remainder operator, %: % divides and keeps only what's left over. 17 % 5 is 2, because 5 fits into 17 three times (15) with 2 left. It works with whole numbers — floats are cut to ints first; for float remainders there's fmod(). The result takes the sign of the first number: -7 % 3 is -1. That sounds niche, but you'll use % all the time: "is this number even?" ($n % 2 === 0), "every third row", and turning minutes into hours and minutes.

The remainder operator
<?php
var_dump(17 % 5); // int(2)
var_dump(-7 % 3); // int(-1): takes the sign of the first number
var_dump(8 % 2 === 0); // bool(true): 8 is even
var_dump(fmod(7.5, 2)); // float(1.5): remainder for floats

Fixing the end time: that's Mia's bug. She stored times as minutes since midnight — 9:30 is 570 — which is a good idea, because minutes are easy to add. But to show 615 minutes as a time, dividing by 60 gives 10.25 hours, a float. What she needs is the whole hours, intdiv(615, 60) = 10, and the leftover minutes, 615 % 60 = 15. Together: 10:15. (Showing 10:05 with a zero in front needs string formatting, which Act 07 covers.)

Mia's end-time bug, and the fix
<?php
$startMinutes = 570; // 9:30 = 9 × 60 + 30
$endMinutes = $startMinutes + 45; // 615
echo $endMinutes / 60, ":00\n"; // 10.25:00 — wrong
$hours = intdiv($endMinutes, 60); // 10: the whole hours
$minutes = $endMinutes % 60; // 15: the minutes left over
echo "$hours:$minutes\n"; // 10:15

Power, and dividing by zero: ** raises a number to a power: 2 ** 3 is 8. One trap: -2 ** 2 is -4, because PHP works out the power first and the minus after; write (-2) ** 2 to get 4. And a rule for / and %: dividing by zero is impossible, so PHP stops with a DivisionByZeroError ("Division by zero" or "Modulo by zero"). Whenever the number you divide by comes from data, check that it isn't zero first.

Power, a minus trap, and dividing by zero
<?php
var_dump(2 ** 10); // int(1024)
var_dump(-2 ** 2); // int(-4): the power is worked out first
var_dump((-2) ** 2); // int(4)
$people = 0;
echo 5000 / $people;
// Fatal error: Uncaught DivisionByZeroError: Division by zero

Assignment returns a value: you know = from Act 03: it stores the right side in the variable on the left. What's new is that an assignment is itself an expression — its value is the value that was stored. That's why $a = $b = 0; works: $b = 0 gives 0, which is then stored in $a. You can use this inside bigger expressions too, but it's hard to read, so keep assignments on their own line.

Compound assignment: very often you change a variable based on its own value: $total = $total + $price;. PHP has a shortcut for every arithmetic operator — $total += $price; means exactly the same. These are called compound assignment operators: +=, -=, *=, /=, %= and **=. There's also .=, which adds text to the end of a string (chapter 4), and ??= (chapter 6). The table below lists them.

Table — Arithmetic and assignment operators
OperatorExampleResult / same as
+ - * /25 + 10, 7 / 235, 3.5
% (remainder)17 % 52
** (power)2 ** 101024
+=$total += 500$total = $total + 500
-= *= /= %= **=$price *= 2$price = $price * 2
.=$message .= "!"$message = $message . "!"
++ / --$count++$count = $count + 1
Compound assignment
<?php
$totalPence = 2500; // a haircut
$totalPence += 1000; // + a beard trim: 3500
$totalPence -= 500; // - a £5 voucher: 3000
$totalPence *= 2; // two people: 6000
echo $totalPence, "\n"; // 6000

Increment and decrement: adding or subtracting exactly 1 is so common that it has its own operators: $count++ adds 1, $count-- subtracts 1. The position matters when you use the result in the same expression. Post-increment, $a++, gives the old value, then adds 1. Pre-increment, ++$a, adds 1 first and gives the new value. Used on a line of its own, both do the same thing. Two warnings from PHP 8.5: ++ on text (like "a") is deprecated, and -- on null does nothing. Use ++ and -- only on numbers.

Post-increment vs. pre-increment
<?php
$bookings = 5;
$before = $bookings++; // gives the old value (5), then adds 1
var_dump($before, $bookings); // int(5) int(6)
$after = ++$bookings; // adds 1 first (7), then gives it
var_dump($after, $bookings); // int(7) int(7)

Key Takeaway

An operator works on operands to produce a value. Arithmetic: add, subtract, multiply, divide, % (remainder) and power. % gives the remainder of whole-number division — with intdiv() it turns minutes into hours and minutes. Dividing by zero throws DivisionByZeroError. Compound assignments (+=, -=, .=, …) shorten $x = $x op y, and ++/-- add or subtract 1: $a++ gives the old value, ++$a the new one.

Why This Matters

Arithmetic is everywhere in a booking system: prices, totals, durations, end times. Knowing that % and intdiv() split minutes into hours, that / can give a float, and that dividing by zero stops the file, means Mia gets times and totals right the first time.

And the vocabulary — operator, operand, expression — is what the rest of this Act builds on, all the way to the line that caused the double booking.

The end times are fixed. Next on Marcus's list: the morning-slots report, which should list every slot before 12:00. It shows 11:00 and 11:30 — but not 9:00, 9:30 or 10:00. The check is simple: $slot < "12:00". "How can 9:30 not be smaller than 12:00?" That's the next chapter.