Updated June 9, 2023
Introduction to Arithmetic Operators in PHP
Arithmetic operators are one of the types of operators in the PHP programming language. The arithmetic operators are handy for many mathematical calculations to do different types of programs to ease our problem-solving tasks. Arithmetic operators can be used only with the numerical values/numbers to perform arithmetic operations like Addition (+), Subtraction (-), Multiplication (x), Division (/), Modulus (%), Exponentiation (**)(1st number to the power of 2nd number ).
Explanation of Arithmetic operators in PHP
The Arithmetic operators are used two numeric values/numbers to perform Addition (+), Subtraction (-), Multiplication (x), Division (/), Modulus (%), Exponentiation (**)(1st number to the power of 2nd number ).
1. Addition (+)
The Addition is one of the Arithmetic operators of the PHP programming language. It requires at least two/ more numerical values/numbers to add.
Code:
<?php
$pavan = 25;
$kumar = 24;
$sake = $pavan + $kumar;
echo "First Variable Value : $pavan";
echo "<br/>";
echo "Second Variable Value : $kumar";
echo "<br/>";
echo "The sum of the two variables :: ";
echo $sake;
echo "<br/>";
echo "<hr/>";
$p1 = 23;
echo "Third Variable Value : $p1";
echo "<br/>";
$k1 = $sake + $p1 ;
echo "The Sum of the three Variables :: $k1";
echo "<br/>";
echo "<hr/>";
$pavan1 = 44;
echo "Fourth Variable Value : $pavan1";
echo "<br/>";
$s1 = $pavan1 + $k1;
echo "The Sum of the four Variables :: $s1";
echo "<br/>";
?>
Output:
2. Subtraction (-)
Subtraction is one of the Arithmetic operators of the PHP programming language. It requires at least two/ more numerical values/numbers to subtract.
Code:
<?php
$pavan = 25;
$kumar = 24;
$sake = $pavan - $kumar;
echo "First Variable Value : $pavan";
echo "<br/>";
echo "Second Variable Value : $kumar";
echo "<br/>";
echo "The substraction of the two variables :: ";
echo $sake;
echo "<br/>";
echo "<hr/>";
$p1 = 23;
echo "Third Variable Value : $p1";
echo "<br/>";
$k1 = $sake - $p1 ;
echo "The Substraction of the three Variables :: $k1";
echo "<br/>";
echo "<hr/>";
$pavan1 = 44;
echo "Fourth Variable Value : $pavan1";
echo "<br/>";
$s1 = $k1 - $pavan1;
echo "The Substraction of the four Variables $pavan, $kumar, $p1, $k1, $pavan1 :: $s1";
echo "<br/>";
?>
Output:
3. Multiplication (x)
Multiplication is one of the Arithmetic operators of the PHP programming language. It requires at least two/ more numerical values/numbers to multiply.
Code:
<?php
$pavan = 25;
$kumar = 24;
$sake = $pavan * $kumar;
echo "First Variable Value : $pavan";
echo "<br/>";
echo "Second Variable Value : $kumar";
echo "<br/>";
echo "The multiplicaation of the two variables :: ";
echo $sake;
echo "<br/>";
echo "<hr/>";
$p1 = 23;
echo "Third Variable Value : $p1";
echo "<br/>";
$k1 = $sake * $p1 ;
echo "The Multiplication of the three Variables :: $k1";
echo "<br/>";
echo "<hr/>";
$pavan1 = 44;
echo "Fourth Variable Value : $pavan1";
echo "<br/>";
$s1 = $pavan1 * $k1;
echo "The Multiplication of the four Variables :: $s1";
echo "<br/>";
?>
Output:
4. Division (/)
The division is one of the Arithmetic operators of the PHP programming language. It requires at least two/ more numerical values/numbers to divide.
Code:
<?php
$pavan = 250;
$kumar = 5;
$sake = $pavan / $kumar;
echo "First Variable Value : $pavan";
echo "<br/>";
echo "Second Variable Value : $kumar";
echo "<br/>";
echo "The Division of the two variables :: ";
echo $sake;
echo "<br/>";
echo "<hr/>";
$p1 = 5;
echo "Third Variable Value : $p1";
echo "<br/>";
$k1 = $sake / $p1 ;
echo "The Division of the three Variables :: $k1";
echo "<br/>";
echo "<hr/>";
$pavan1 = 5;
echo "Fourth Variable Value : $pavan1";
echo "<br/>";
$s1 = $k1/$pavan1;
echo "The Division of the four Variables :: $s1";
echo "<br/>";
?>
Output:
5. Modulus (%)
Modulus is one of the Arithmetic operators of this programming language. Knowing the remainder value requires at least two/ more numerical values/numbers. You will get only number 1 or number 0 values as the remainder when you divide a number with any other number, but if you divide with a big number, you will definitely get a 0 value for that.
Code:
<?php
$pavan = 250;
$kumar = 5;
$sake = $pavan % $kumar;
echo "First Variable Value : $pavan";
echo "<br/>";
echo "Second Variable Value : $kumar";
echo "<br/>";
echo "The remainder by dividing $pavan with $kumar :: ";
echo $sake;
echo "<br/>";
echo "<hr/>";
$p1 = 3;
echo "Third Variable Value : $p1";
echo "<br/>";
$k1 = $pavan%$p1 ;
echo "The remainder by dividing $pavan with $p1 :: $k1";
echo "<br/>";
echo "<hr/>";
$pavan1 = 5;
echo "Fourth Variable Value : $pavan1";
echo "<br/>";
$s1 = $pavan%$pavan1;
echo "The remainder by dividing $pavan with $pavan1 :: $s1";
echo "<br/>";
?>
Output:
6. Exponentiation (**)
Exponentiation is one of the Arithmetic operators of this programming language. It requires at least two/ more numerical values/numbers to know the 1st number to the power of the 2nd number.
Code:
<?php
$pavan = 2;
$kumar = 5;
$sake = $pavan ** $kumar;
echo "First Variable Value : $pavan";
echo "<br/>";
echo "Second Variable Value : $kumar";
echo "<br/>";
echo "The result by getting the power of $pavan with $kumar :: ";
echo $sake;
echo "<br/>";
echo "<hr/>";
$p1 = 3;
echo "Third Variable Value : $p1";
echo "<br/>";
$k1 = $pavan**$p1 ;
echo "The result by getting the power of $pavan with $p1 :: $k1";
echo "<br/>";
echo "<hr/>";
$pavan1 = 5;
echo "Fourth Variable Value : $pavan1";
echo "<br/>";
$s1 = $pavan**$pavan1;
echo "The result by getting the power of $pavan with $pavan1 :: $s1";
echo "<br/>";
?>
Output:
Conclusion
I hope you understand what the Arithmetic operators are and how to use those Arithmetic operators. I think you also understood what is meant by Addition (+), Subtraction (-), Multiplication (x), Division (/), Modulus (%), Exponentiation (**)(1st number to the power of 2nd number ).
Recommended Articles
This is a guide to Arithmetic Operators in PHP. Here we discuss the detailed explanation of arithmetic operators with code implementation. You can also go through our suggested articles to learn more –