← Back to Chapters

PHP Arithmetic Operators

➕ PHP Arithmetic Operators

? Quick Overview

Arithmetic operators are used to perform common mathematical operations on variables and values in PHP.

? Key Concepts

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (remainder)
  • ** Exponentiation (power)

? Syntax & Theory

PHP arithmetic operators work on numeric operands. You can combine multiple operators in one expression, and PHP follows standard mathematical precedence rules.

? Live Interactive Demo

Test how variables $a and $b interact!

Addition (+)13
Subtraction (-)7
Multiplication (*)30
Division (/)3.33
Modulus (%)1
Exponent (**)1000

? Code Example

? View Code Example
// Demonstrating all PHP arithmetic operators
<?php
$a = 10;
$b = 3;

echo "Addition: " . ($a + $b) . "<br>";
echo "Subtraction: " . ($a - $b) . "<br>";
echo "Multiplication: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b) . "<br>";
echo "Modulus: " . ($a % $b) . "<br>";
echo "Exponentiation: " . ($a ** $b);
?>

? Live Output / Explanation

The script performs arithmetic operations on two numbers ($a = 10 and $b = 3) and prints each result using echo.

? Interactive Understanding

Change the values of $a and $b and refresh the page to instantly see how different numbers affect each operation.

? Use Cases

  • Calculating totals, discounts, and taxes
  • Building calculators and billing systems
  • Handling scores, percentages, and analytics

✅ Tips & Best Practices

  • Use parentheses () to improve readability and ensure correct calculation.
  • Always validate inputs before performing division.
  • Remember that ** is available from PHP 5.6 onward.

? Try It Yourself

  • Create a file named arithmetic.php.
  • Define two variables and apply all arithmetic operators.
  • Display results using echo.
  • Experiment with negative and decimal values.