How PHP Arithmetic Operators Handle Division, Remainders, and Precedence

PHP uses +, -, *, and / for basic arithmetic. Use intdiv() and % for an integer quotient and remainder, and ** for exponentiation. Operator precedence and parentheses determine how a formula with several operators is grouped.

Basic arithmetic operators and results

Of the arithmetic operators in the PHP manual, unary + leaves a number unchanged and unary - reverses its sign. The examples start with $a set to 10 and $b set to 4. var_dump() shows the value and type, such as int for an integer and float for a floating-point number.

Save each block in a separate PHP file. With PHP 8 installed, run it from a terminal using its filename, for example php arithmetic.php.

<?php
$a = 10;
$b = 4;

var_dump(+$a, -$a);
var_dump($a + $b, $a - $b, $a * $b);
var_dump($a / $b, $a % $b);
var_dump($b ** 3);
Expression Operation Value
+$a Keep the sign 10
-$a Reverse the sign -10
$a + $b Addition 14
$a - $b Subtraction 6
$a * $b Multiplication 40
$a / $b Division 2.5
$a % $b Integer remainder 2
$b ** 3 Exponentiation 64

The last expression, $b ** 3, raises 4 to the third power and gives 64. To store a result back in the same variable, use a PHP assignment operator such as +=.

Choose between division and an integer quotient

With two integer operands, / gives an integer for exact division and a float when there is a remainder. A floating-point operand such as 12.0 produces a float. To keep just the integer quotient, use intdiv(), which truncates toward zero.

<?php
var_dump(10 / 4);
var_dump(12 / 4);
var_dump(12.0 / 4);

var_dump(intdiv(10, 4), 10 % 4);

10 / 4 gives float(2.5) and 12 / 4 gives int(3). If you pack 10 items in groups of four, intdiv(10, 4) gives two complete groups and 10 % 4 gives the two items left over.

In PHP 8, a zero divisor with /, %, or intdiv() throws DivisionByZeroError. If the divisor comes from input, check for zero before calculating.

Find the remainder with negative numbers and decimals

% converts both operands to integers and calculates an integer remainder. A nonzero remainder has the same sign as the dividend, the number being divided. Use fmod() if the remainder calculation needs to keep the fractional parts.

<?php
var_dump(intdiv(-10, 4));
var_dump(-10 % 4, 10 % -4);
var_dump(fmod(10.5, 4.0));

intdiv(-10, 4) gives -2. -10 % 4 gives -2, 10 % -4 gives 2, and fmod(10.5, 4.0) gives float(2.5).

Group operations with precedence and parentheses

Under PHP’s operator precedence rules, multiplication, division, and remainder group before addition and subtraction. A chain of *, /, and % groups from the left, as does a chain of + and -. Put parentheses around an expression to group it explicitly.

<?php
var_dump(4 + 3 * 2, (4 + 3) * 2);
var_dump(4 * 3 / 2, 4 + 3 - 2);
var_dump(2 ** 3 ** 2);
var_dump(-2 ** 2, (-2) ** 2);

4 + 3 * 2 gives 10, while (4 + 3) * 2 gives 14. At equal precedence, 4 * 3 / 2 gives 6 and 4 + 3 - 2 gives 5.

Exponentiation groups from the right, so 2 ** 3 ** 2 means 2 ** (3 ** 2) and gives 512. The power binds more tightly than unary minus, so -2 ** 2 gives -4. To square the negative number, use (-2) ** 2, which gives 4.