• PHP Video Tutorials

PHP - Arithmetic Operators Examples



In PHP, arithmetic operators are used to perform mathematical operations on numeric values. The following table highligts 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

Example

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

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

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

   $c = $a - $b;
   echo "Substraction 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";
?>

It will produce the following output

Addtion Operation Result: 62 
Substraction Operation Result: 22 
Multiplication Operation Result: 840 
Division Operation Result: 2.1 
Modulus Operation Result: 2 
Increment Operation Result: 42 
Decrement Operation Result: 43
Advertisements