Web Analytics

PHP Arithmetic Operators

Beginner~20 min read

Arithmetic operators perform mathematical operations on numbers. PHP supports all standard math operations plus some powerful extras like exponentiation. Let's master them all!

Arithmetic Operators Overview

Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulus 10 % 3 1
** Exponentiation 2 ** 3 8
- Negation -10 -10
Output
Click Run to execute your code

Addition (+)

Adds two numbers together:

<?php
$a = 10;
$b = 5;
$sum = $a + $b;
echo $sum;  // Output: 15

// Works with floats
$price1 = 19.99;
$price2 = 5.50;
$total = $price1 + $price2;
echo $total;  // Output: 25.49
?>

Subtraction (-)

Subtracts the second number from the first:

<?php
$a = 10;
$b = 5;
$difference = $a - $b;
echo $difference;  // Output: 5

// Can result in negative numbers
$result = 5 - 10;
echo $result;  // Output: -5
?>

Multiplication (*)

Multiplies two numbers:

<?php
$quantity = 5;
$price = 10.50;
$total = $quantity * $price;
echo $total;  // Output: 52.5

// Multiplying by zero
$result = 100 * 0;
echo $result;  // Output: 0
?>

Division (/)

Divides the first number by the second:

<?php
$total = 100;
$people = 4;
$perPerson = $total / $people;
echo $perPerson;  // Output: 25

// Division can result in floats
$result = 10 / 3;
echo $result;  // Output: 3.3333333333333
?>
Division by Zero: Dividing by zero produces a warning and returns INF (infinity) or NAN (not a number). Always validate divisors!

Modulus (%)

Returns the remainder after division:

<?php
$number = 10;
$divisor = 3;
$remainder = $number % $divisor;
echo $remainder;  // Output: 1 (10 รท 3 = 3 remainder 1)

// Check if number is even or odd
$num = 7;
if ($num % 2 == 0) {
    echo "Even";
} else {
    echo "Odd";  // This executes
}
?>
Common Use Cases:
  • Check if number is even/odd: $n % 2 == 0
  • Cycle through values: $index % $arrayLength
  • Check divisibility: $n % 5 == 0

Exponentiation (**)

Raises the first number to the power of the second (PHP 5.6+):

<?php
$base = 2;
$exponent = 3;
$result = $base ** $exponent;
echo $result;  // Output: 8 (2ยณ = 2 ร— 2 ร— 2)

// Calculate area of square
$side = 5;
$area = $side ** 2;
echo $area;  // Output: 25

// Fractional exponents (roots)
$number = 16;
$squareRoot = $number ** 0.5;
echo $squareRoot;  // Output: 4
?>

Negation (-)

Changes the sign of a number:

<?php
$positive = 10;
$negative = -$positive;
echo $negative;  // Output: -10

// Double negation returns original
$result = -(-10);
echo $result;  // Output: 10
?>

Operator Precedence

When multiple operators are used, PHP follows mathematical order of operations:

Priority Operators Description
1 (Highest) ** Exponentiation
2 - Negation (unary)
3 * / % Multiplication, Division, Modulus
4 (Lowest) + - Addition, Subtraction
<?php
// Without parentheses
$result = 2 + 3 * 4;
echo $result;  // Output: 14 (not 20!)
// Calculation: 3 * 4 = 12, then 2 + 12 = 14

// With parentheses
$result = (2 + 3) * 4;
echo $result;  // Output: 20
// Calculation: 2 + 3 = 5, then 5 * 4 = 20

// Complex expression
$result = 2 ** 3 + 4 * 5;
echo $result;  // Output: 28
// Calculation: 2ยณ = 8, 4 * 5 = 20, 8 + 20 = 28
?>
Best Practice: Use parentheses to make your intentions clear, even when not strictly necessary. It improves code readability!

Common Mistakes

1. Division by zero

<?php
$result = 10 / 0;  // โŒ Warning: Division by zero

// Always check first
$divisor = 0;
if ($divisor != 0) {
    $result = 10 / $divisor;
} else {
    echo "Cannot divide by zero";
}
?>

2. Integer division expectations

<?php
$result = 7 / 2;
echo $result;  // 3.5 (not 3!)

// For integer division, use intdiv() (PHP 7+)
$result = intdiv(7, 2);
echo $result;  // 3
?>

3. Modulus with negative numbers

<?php
echo 10 % 3;   // 1
echo -10 % 3;  // -1 (takes sign of dividend)
echo 10 % -3;  // 1
?>

Summary

  • Addition (+): Add two numbers
  • Subtraction (-): Subtract second from first
  • Multiplication (*): Multiply two numbers
  • Division (/): Divide first by second
  • Modulus (%): Get remainder after division
  • Exponentiation (**): Raise to power (PHP 5.6+)
  • Negation (-): Change sign
  • Precedence: ** > * / % > + -
  • Parentheses: Override precedence

What's Next?

Now that you've mastered arithmetic operators, let's learn about Assignment Operators - shortcuts for updating variables with arithmetic operations!