PHP Arithmetic Operators Explained with Examples
What Are PHP Arithmetic Operators?
In every programming language, arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and more.
In PHP, arithmetic operators work just like they do in mathematics — they take numerical values (variables or constants) as operands and return a numeric result.
Understanding these operators is essential for writing programs that perform calculations, billing systems, financial analysis, scientific computations, and even AI-based logic building.
List of PHP Arithmetic Operators
| Operator | Description | Example | Output |
|---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y |
- | Subtraction | $x - $y | Difference of $x and $y |
* | Multiplication | $x * $y | Product of $x and $y |
/ | Division | $x / $y | Quotient of $x divided by $y |
% | Modulus | $x % $y | Remainder of $x divided by $y |
** | Exponentiation | $x ** $y | $x raised to the power $y |
Example: Using PHP Arithmetic Operators
Let’s write a simple PHP program that demonstrates all arithmetic operators.
<?php
$x = 20;
$y = 6;
echo "Addition (+): " . ($x + $y) . "<br>";
echo "Subtraction (-): " . ($x - $y) . "<br>";
echo "Multiplication (*): " . ($x * $y) . "<br>";
echo "Division (/): " . ($x / $y) . "<br>";
echo "Modulus (%): " . ($x % $y) . "<br>";
echo "Exponentiation (**): " . ($x ** $y) . "<br>";
?>
Output:
Addition (+): 26 Subtraction (-): 14 Multiplication (*): 120 Division (/): 3.3333333333 Modulus (%): 2 Exponentiation (**): 64000000 PHP Addition Operator (+)
Used to add two or more numbers or variables.
Example:
<?php
$a = 10;
$b = 15;
$sum = $a + $b;
echo "The sum is: $sum";
?>
Output:
The sum is: 25PHP Subtraction Operator (-)
Used to subtract one number from another.
<?php
$a = 50;
$b = 20;
echo $a - $b;
?>
Output:
30PHP Multiplication Operator (*)
Used to multiply two numbers.
Example:
<?php
$price = 200;
$qty = 3;
$total = $price * $qty;
echo "Total Price: ₹$total";
?>
Output:
Total Price: ₹600PHP Division Operator (/)
Used to divide one number by another.
Example:
<?php
$total = 500;
$people = 4;
$share = $total / $people;
echo "Each person gets ₹$share";
?>
Output:
Each person gets ₹125PHP Modulus Operator (%)
Returns the remainder after division — useful for logic building (like checking even or odd numbers).
Example:
<?php
$number = 9;
if ($number % 2 == 0) {
echo "Even Number";
} else {
echo "Odd Number";
}
?>
Output:
Odd NumberPHP Exponentiation Operator ()**
Introduced in PHP 5.6, this operator raises one number to the power of another.
Example:
<?php
echo 5 ** 3; // 5³ = 125
?>
Output:
125Real-Life Example: Basic Calculator in PHP
Let’s build a small calculator using arithmetic operators.
<?php
$num1 = 15;
$num2 = 5;
echo "Addition: " . ($num1 + $num2) . "<br>";
echo "Subtraction: " . ($num1 - $num2) . "<br>";
echo "Multiplication: " . ($num1 * $num2) . "<br>";
echo "Division: " . ($num1 / $num2) . "<br>";
echo "Modulus: " . ($num1 % $num2);
?>
Output:
Addition: 20 Subtraction: 10 Multiplication: 75 Division: 3 Modulus: 0Arithmetic Operators and Data Types
PHP automatically converts string numbers during arithmetic operations.
Example:
<?php
$x = "100";
$y = 25;
echo $x + $y; // PHP automatically converts "100" to integer
?>
Output:
125