Arithmetic
What you'll learn:
- calculate with
+,-,*,/,%and**and predict the type of the result; - change a variable with
++,+=and.=and tell$i++from++$i; - compare values with
==and===and explain the PHP 8 rule for numbers and strings; - supply a default value with
??and?:and remember thatandis weaker than=.

Imagine a calculator with house rules. It adds, subtracts and multiplies the way you learned at school, but some of its keys come with conditions. The / key always keeps the fraction, and a separate intdiv key drops it.
In a program, sign keys like + and / are called operators. Most operators stand between two values and tell PHP what to do with them. intdiv, though, is a function: you call it by name and pass the numbers in parentheses. In the theme on types you already met the / operator and know that the result of a division is not always whole.
Take a problem from real life: 17 pies go into boxes, and each box holds 5. How many boxes fill up completely, and how many pies are left over?
<?php
$pies = 17;
echo $pies / 5, "\n"; // 3.4
echo intdiv($pies, 5), "\n"; // 3
echo $pies % 5, "\n"; // 2Let's go through the program line by line:
$pies / 5prints3.4: ordinary division keeps the fraction.intdiv($pies, 5)divides without the fraction and returns3, whichechoprints: that is the number of boxes that fill up completely.$pies % 5prints2: these two pies did not fit into any full box.
Four operations and a power
PHP's arithmetic operators:
+adds;-subtracts;*multiplies;/divides and keeps the fraction;%gives the remainder of a division;**raises to a power:2 ** 10is1024.
The type of the result depends on the values. The sum of two int values is an int again, and if one of the numbers is a float, the result is a float too: 2 + 1.0 gives float(3).
Division has its own behavior. 10 / 5 gives int(2) because it divides evenly, while 7 / 2 gives float(3.5). In many languages dividing integers drops the fraction, but PHP has a separate function for that.
Whole division and the remainder
The intdiv function
intdiv($a, $b) divides one integer by another and drops the fraction. It does not round:
intdiv(17, 5)gives3, and3.4becomes3;intdiv(19, 5)gives3as well, although3.8is closer to four;- for negative numbers the fraction is cut off too:
intdiv(-7, 2)gives-3.
The % operator
The % operator gives the remainder, what did not fit into the full boxes. A remainder of 0 means the number divided evenly: 10 % 2 gives 0, and that is how you check that a number is even.
A remainder can be negative: its sign matches the sign of the dividend, the number on the left. So -7 % 3 gives -1, and 7 % -3 gives 1. This is where the box picture stops working: you cannot pack minus seven pies into boxes, yet % works with negative numbers and takes the sign of the dividend.
Order of operations
When an expression holds several operators, PHP runs them by precedence, a fixed order, as in school math:
**comes first;- then
*,/and%; - then
+and-; - last comes the
.operator, which joins two strings into one.
Parentheses change this order. Let's see what we get:
echo 2 + 3 * 4, "\n"; // 14
echo (2 + 3) * 4, "\n"; // 20
echo 'Total: ' . 2 + 3, "\n"; // Total: 5
echo 'a' . 1 + 2, "\n"; // a3On the third line PHP first adds 2 + 3 and then attaches 5 to the string. The fourth line works the same way: 1 + 2 gives 3, and the result is a3. This order holds since PHP 8, and the parentheses in 'Total: ' . (2 + 3) make it visible at a glance.
Interviewers like to give the last example: it checks whether you know that in PHP 8 addition runs before joining.
This is where the calculator stops being an exact picture. You cannot type a string into a real calculator, but PHP joins strings and, in '5' + 3, turns '5' into the number 5 on its own.
Division by zero
You cannot divide by zero, and PHP does not try to guess an answer:
$boxes = 0;
echo $pies / $boxes, "\n"; // ✗
echo 'done', "\n";Fatal error: Uncaught DivisionByZeroError: Division by zeroPHP stops the program at this line: everything printed before it stays on the screen, but the word done never appears. intdiv($pies, 0) and $pies % 0 stop the program the same way.
Sometimes you need infinity instead of a stop. The fdiv function is there for that: fdiv(1, 0) returns INF, a special value of type float.
Common mistake
The average of two numbers is computed without parentheses:
$a = 4;
$b = 6;
$average = $a + $b / 2; // ✗
echo $average, "\n"; // 7Division runs before addition, so PHP computes 4 + 3 and prints 7 instead of 5. There is no error message, and the wrong number travels on through the program. Put the sum in parentheses: ($a + $b) / 2 gives 5.
Key points
/keeps the fraction andintdivdrops it;10 / 5gives anint, and7 / 2afloat.%gives the remainder, and its sign matches the sign of the dividend:-7 % 3is-1.- First
**, then*,/and%, then+and-, and joining with.last; parentheses change the order. - Division by zero stops the program with
DivisionByZeroError, whilefdiv(1, 0)returnsINF.