How to Use PHP Arithmetic Operators: Math Calculations Explained

What are PHP Arithmetic Operators?

In programming, an operator is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation. PHP provides a built-in set of arithmetic operators that function exactly like the buttons on a calculator.

If you have already learned how to store data from our previous lesson on PHP variables (INSERT LINK HERE FOR PHP VARIABLES TUTORIAL), you are ready to start manipulating that data.

1. The Addition Operator: +

The addition operator (+) is used to sum two numbers. It is incredibly straightforward.

<?php
    echo 15 + 5; // Output: 20

    $base_price = 100;
    $shipping_fee = 15;
    $total = $base_price + $shipping_fee;
    
    echo "Total to pay: $" . $total; // Output: Total to pay: $115
?>

⚠️ Pro Tip: In languages like JavaScript, the + symbol is also used to join text (concatenate strings). Not in PHP! In PHP, math is math. To join strings, we use the dot (.) operator.

2. The Subtraction Operator: -

The subtraction operator (-) finds the difference between two values. It is heavily used in date calculations and inventory management.

<?php
    $current_year = 2026;
    $birth_year = 1995;
    
    $age = $current_year - $birth_year;
    echo "The user is $age years old."; // Output: The user is 31 years old.
?>

3. The Multiplication Operator: *

In computer science, we do not use "x" or a dot for multiplication. We use the asterisk (*).

<?php
    $item_price = 50;
    $quantity = 3;
    
    $subtotal = $item_price * $quantity;
    echo "Subtotal: $" . $subtotal; // Output: Subtotal: $150
?>

4. The Division Operator: /

For division, PHP uses the forward slash (/).

A great feature of PHP is its intelligent type juggling. If you divide two integers and the result is a whole number, PHP returns an Integer. If there is a fractional remainder, PHP automatically converts the result into a Float (decimal).

<?php
    echo 10 / 2; // Output: 5 (Integer)
    echo 10 / 3; // Output: 3.3333333333333 (Float)
?>

5. The Modulo Operator (Remainder): %

This is often the most confusing operator for beginners, but it is one of the most powerful tools in a programmer's arsenal. The modulo operator (%) does not divide the numbers; it performs a division and returns only the remainder.

[Image of parts of a long division: dividend, divisor, quotient, remainder]

The modulo operator specifically grabs the "Remainder" at the bottom of the division.

<?php
    echo 10 % 2; // Output: 0 (10 divided by 2 is 5, with exactly 0 remaining)
    echo 10 % 3; // Output: 1 (10 divided by 3 is 3, with 1 remaining)
?>

Why is Modulo so important?

Modulo is universally used in programming to check if a number is even or odd. If a number is even, $number % 2 will always return 0. If it is odd, it will return 1. You will use this logic frequently to create alternating row colors in HTML tables (Zebra striping) or to distribute tasks evenly in algorithms.

6. The Exponentiation Operator: **

Introduced in modern PHP, the double asterisk (**) allows you to raise a number to the power of another easily, without needing to call complex math functions.

<?php
    echo 2 ** 3; // This means 2³ (2 * 2 * 2). Output: 8
    echo 5 ** 2; // This means 5² (5 * 5). Output: 25
?>

🚧 Common Errors and Best Practices

  • DivisionByZeroError: In mathematics, you cannot divide a number by zero. If you try to run echo 10 / 0;, your PHP application will crash and throw a fatal error. Always ensure your divisor is not zero before calculating!
  • Operator Precedence (PEMDAS): PHP follows the standard order of operations. Multiplication and division are executed before addition and subtraction. For example, 2 + 3 * 5 results in 17, not 25. If you want the addition to happen first, use parentheses: (2 + 3) * 5.

Ready for the Next Challenge?

Now that you can calculate values, you need to learn how to assign them efficiently and compare them to make logical decisions. Join us in the next lesson to master Assignment and Comparison Operators!

Go to the Next Lesson in the Syllabus →

Nenhum comentário:

Veja também: