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

OperatorDescriptionExampleOutput
+Addition$x + $ySum of $x and $y
-Subtraction$x - $yDifference of $x and $y
*Multiplication$x * $yProduct of $x and $y
/Division$x / $yQuotient of $x divided by $y
%Modulus$x % $yRemainder 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: 25

PHP Subtraction Operator (-)

Used to subtract one number from another.

<?php
$a = 50;
$b = 20;
echo $a - $b;
?>

Output:

 
30

PHP Multiplication Operator (*)

Used to multiply two numbers.

Example:

<?php
$price = 200;
$qty = 3;
$total = $price * $qty;
echo "Total Price: ₹$total";
?>

Output:

 
Total Price: ₹600

PHP 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 ₹125

PHP 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 Number

PHP 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:

 
125

Real-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: 0

Arithmetic 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

Read More

How to Create Database in MySQL

  • By admin
  • November 27, 2021
  • 65 views
How to Create Database in MySQL

How to create table in MySQL

  • By admin
  • November 6, 2021
  • 46 views
How to create table in MySQL

MySQL commands with examples

  • By admin
  • September 11, 2021
  • 137 views
MySQL commands with examples

MySQL use database

  • By admin
  • May 28, 2021
  • 45 views
MySQL use database

System Software

  • By admin
  • May 20, 2021
  • 66 views

Introduction to software

  • By admin
  • May 13, 2021
  • 56 views
Introduction to software