Home >Backend Development >PHP Tutorial >Arithmetic Operators in PHP

Arithmetic Operators in PHP

王林
王林Original
2024-08-29 12:38:37673browse

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 ).

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Arithmetic Operators in PHP

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:

Arithmetic Operators in PHP

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:

Arithmetic Operators in PHP

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:

Arithmetic Operators in PHP

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:

Arithmetic Operators in PHP

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:

Arithmetic Operators in PHP

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 ).

The above is the detailed content of Arithmetic Operators in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP OperatorsNext article:PHP Operators