Skip to content
Theory · Operators and comparison · lesson 1 of 8

Arithmetic

What you'll learn

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 that and is weaker than =.
Seventeen pies boxed five to a box — three full boxes intdiv, two left over % 5 = 2, a calculator showing 3.4

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
<?php

$pies = 17;
echo $pies / 5, "\n"; // 3.4
echo intdiv($pies, 5), "\n"; // 3
echo $pies % 5, "\n"; // 2

Let's go through the program line by line:

  1. $pies / 5 prints 3.4: ordinary division keeps the fraction.
  2. intdiv($pies, 5) divides without the fraction and returns 3, which echo prints: that is the number of boxes that fill up completely.
  3. $pies % 5 prints 2: 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 ** 10 is 1024.

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) gives 3, and 3.4 becomes 3;
  • intdiv(19, 5) gives 3 as well, although 3.8 is 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:

  1. ** comes first;
  2. then *, / and %;
  3. then + and -;
  4. last comes the . operator, which joins two strings into one.

Parentheses change this order. Let's see what we get:

PHP
echo 2 + 3 * 4, "\n"; // 14
echo (2 + 3) * 4, "\n"; // 20
echo 'Total: ' . 2 + 3, "\n"; // Total: 5
echo 'a' . 1 + 2, "\n"; // a3

On 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:

PHP
$boxes = 0;
echo $pies / $boxes, "\n"; // ✗
echo 'done', "\n";
TEXT
Fatal error: Uncaught DivisionByZeroError: Division by zero

PHP 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:

PHP
$a = 4;
$b = 6;
$average = $a + $b / 2; // ✗
echo $average, "\n"; // 7

Division 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 and intdiv drops it; 10 / 5 gives an int, and 7 / 2 a float.
  • % gives the remainder, and its sign matches the sign of the dividend: -7 % 3 is -1.
  • First **, then *, / and %, then + and -, and joining with . last; parentheses change the order.
  • Division by zero stops the program with DivisionByZeroError, while fdiv(1, 0) returns INF.