← Back to PHP Course | Chapter 3: Operators | Lesson 1 of 8

PHP Arithmetic Operators

Arithmetic operators are the math buttons of a calculator: add, subtract, multiply, divide, and find a remainder. PHP uses them to do sums with your numbers and variables.
Syntax
php
$result = $a + $b;   // addition
$result = $a - $b;   // subtraction
$result = $a * $b;   // multiplication
$result = $a / $b;   // division
$result = $a % $b;   // remainder
$result = $a ** $b;  // power

Addition and Subtraction

+ and - perform the addition and subtraction you'd expect on numbers, and PHP will automatically convert numeric-looking strings ("5" + 3) to numbers before operating — a convenience that's handy for form input but worth being aware of, since it can mask a typo that produced a non-numeric string.

Example: Addition and Subtraction

php
<?php
// Print `10 + 5, "\n"` to the output
echo 10 + 5, "\n";
// Print `10 - 5, "\n"` to the output
echo 10 - 5, "\n";
// Print `"5" + 3` to the output
echo "5" + 3;
?>

Multiplication and Division

* multiplies two values, and / divides them — but PHP's division always returns a float the moment the result isn't a whole number, even if both operands were integers.

10 / 3 gives 3.3333..., not an integer with a dropped remainder, which surprises people coming from languages that do integer division by default.

Example: Multiplication and Division

php
<?php
// Print `4 * 3, "\n"` to the output
echo 4 * 3, "\n";
// Print `10 / 3` to the output
echo 10 / 3;
?>

Modulo Operator

% returns the remainder left over after dividing two integers, which makes it the standard tool for checking parity ($n % 2 === 0 for even) or for cycling through a fixed range of values, like wrapping an index back to zero once it passes the end of an array.

Example: Modulo Operator

php
<?php
// Declare `$n`, set to `7`
$n = 7;
// Print `$n % 2 === 0 ? "even" : "odd"` to the output
echo $n % 2 === 0 ? "even" : "odd";
?>

Exponentiation Operator

** raises the left-hand number to the power of the right-hand one — 2 ** 8 evaluates to 256. It's PHP's direct replacement for the older, clunkier pow() function call for this exact purpose, and reads more naturally in a formula.

Example: Exponentiation Operator

php
<?php
// Print `2 ** 8` to the output
echo 2 ** 8;
?>

Increment and Decrement Operators

++ and -- add or subtract exactly 1 from a variable in place.

Written before the variable (++$x) they update the value and then return it; written after ($x++) they return the *original* value first and only then apply the update — a distinction that matters the moment you use the result of the expression itself.

Example: Increment and Decrement Operators

php
<?php
$x = 5;
echo ++$x . "\n"; // pre-increment: 6
$y = 5;
echo $y++ . "\n"; // post-increment: 5
echo $y; // now 6
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting 10 / 4 to give 2, when PHP's / returns the float 2.5; use intdiv() for whole-number division.
  2. Using % with floats and expecting a decimal remainder, when % converts both operands to integers first, so 5.7 % 2 gives 1.
  3. Dividing by zero, which throws a DivisionByZeroError in PHP 8 instead of returning a value.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.