PHP Program to perform arithmetic operations


February 6, 2022, Learn eTutorial
1732

This program will help you to understand how to perform arithmetic operations in PHP programming.

For a better understanding of this example, we always recommend you to learn the basic topics of PHP programming listed below:

Arithmetic operations include

  • Addition (+): Return the sum of the given digits
  • Subtraction (-): Return the difference of the given digits
  • Multiplication (*): Return the product of the given digits
  • Division (/): Return the quotient of the given digits
  • Modulo (%): Return the remainder of 1st digit divided by 2nd digit
  • Exponentiation (**): Return the result of raising the 1st digit to the 2nd digit power.

ALGORITHM

STEP 1: Declare two variables and assign values to them.

STEP 2: Perform the addition of two variables and display the result.

STEP 3: Perform the subtraction of two variables and display the result.

STEP 4: Perform the multiplication of two variables and display the result.

STEP 5: Perform the division of two variables and display the result.

STEP 6: Perform the modulo of two variables and display the result.

STEP 7: Perform the exponentiation of two variables and display the result.

 

PHP Source Code

                                           <?php 
$x=20; 
$y=10; 
$z=$x+$y; 
echo "Sum $x+$y = ",$z, "<br>"; 
$z=$x-$y; 
echo "Difference $x-$y = ",$z, "<br>"; 
$z=$x*$y; 
echo "Product $x*$y = ",$z, "<br>"; 
$z=$x/$y; 
echo "Division $x/$y = ",$z, "<br>"; 
$y=15;
$z=$x%$y; 
echo "Modulo $x%$y = ",$z, "<br>"; 
$y=2;
$z=$x**$y; 
echo "Modulo $x**$y = ",$z, "\n"; 
?> 
                                      

OUTPUT

Sum 20+10 = 30
Difference 20-10 = 10
Product 20*10 = 200
Division 20/10 = 2
Modulo 20 = 5
Modulo 20**2 = 400