PHP - Arithmetic Operators Examples



In PHP, arithmetic operators are used to perform mathematical operations on numeric values. The following table highlights the arithmetic operators that are supported by PHP. Assume variable "$a" holds 42 and variable "$b" holds 20 −

Operator Description Example
+ Adds two operands $a + $b = 62
- Subtracts the second operand from the first $a - $b = 22
* Multiply both the operands $a * $b = 840
/ Divide the numerator by the denominator $a / $b = 2.1
% Modulus Operator and remainder of after an integer division $a % $b = 2
++ Increment operator, increases integer value by one $a ++ = 43
-- Decrement operator, decreases integer value by one $a -- = 42

Basic Usage of Arithmetic Operators

The following example shows how you can use these arithmetic operators in PHP −

<?php
   $a = 42;
   $b = 20;

   $c = $a + $b;
   echo "Addition Operation Result: $c \n";

   $c = $a - $b;
   echo "Subtraction Operation Result: $c \n";

   $c = $a * $b;
   echo "Multiplication Operation Result: $c \n";

   $c = $a / $b;
   echo "Division Operation Result: $c \n";

   $c = $a % $b;
   echo "Modulus Operation Result: $c \n";

   $c = $a++; 
   echo "Increment Operation Result: $c \n";

   $c = $a--; 
   echo "Decrement Operation Result: $c";
?>

Output

It will produce the following output −

Addition Operation Result: 62 
Subtraction Operation Result: 22 
Multiplication Operation Result: 840 
Division Operation Result: 2.1 
Modulus Operation Result: 2 
Increment Operation Result: 42 
Decrement Operation Result: 43

Arithmetic Operations with Negative Numbers

The below PHP program performs basic mathematical operations using two numbers in which one number is negative number. So see how the arithmetic operators perform.

<?php
   $x = -10;
   $y = 5;

   echo "Addition: " . ($x + $y) . "\n";  
   echo "Subtraction: " . ($x - $y) . "\n";  
   echo "Multiplication: " . ($x * $y) . "\n";  
   echo "Division: " . ($x / $y) . "\n";  
   echo "Modulus: " . ($x % $y) . "\n";  
?>

Output

It will generate the following output −

Addition: -5  
Subtraction: -15  
Multiplication: -50  
Division: -2  
Modulus: 0  

Arithmetic Operations with Floating-Point Numbers

This PHP the program performs basic math with decimal numbers. It can do addition, subtraction, multiplicationand division operations on two numbers. The results are displayed using the echo statement.

<?php
   $a = 5.5;
   $b = 2.2;

   echo "Addition: " . ($a + $b) . "\n";
   echo "Subtraction: " . ($a - $b) . "\n";
   echo "Multiplication: " . ($a * $b) . "\n";
   echo "Division: " . ($a / $b) . "\n";
?> 

Output

This will generate the below output −

Addition: 7.7  
Subtraction: 3.3  
Multiplication: 12.1  
Division: 2.5  

Increment and Decrement Operators

Now the below code uses the arithmetic operators to show how the Increment and Decrement operations can be performed.

<?php
   $count = 10;

   echo "Original value: " . $count . "\n";
   echo "After Increment: " . $count++ . "\n";  
   echo "After After Increment: " . $count . "\n";  

   echo "Before Increment: " . ++$count . "\n";  

   echo "Post-Decrement: " . $count-- . "\n";  
   echo "After Post-Decrement: " . $count . "\n";  

   echo "Pre-Decrement: " . --$count . "\n";  
?> 

Output

This will create the below output −

Original value: 10  
After Increment: 10  
After After Increment: 11  
Before Increment: 12  
Post-Decrement: 12  
After Post-Decrement: 11  
Pre-Decrement: 10 
Advertisements