How to use Arithmetic Operator in PHP
In this article, I will explain how the Arithmetic operator can be used in PHP.
PHP Operators
PHP language supports following type of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Arithmetic Operators
The operators are summarized in the following table.
Operator |
Name |
Description |
Example |
Result |
a+b |
Addition |
Sum of a and b |
3+3 |
6 |
a-b |
Subtraction |
Difference of x and y |
10-2 |
8 |
a*b |
Multiplication |
Product of x and y |
2*6 |
12 |
a/b |
Division |
Quotient of x and y |
10/2 |
5 |
a%b |
Modulus |
Remainder of x divided by y |
7%3 |
1 |
-a |
Negation |
Opposite of x |
-5 |
|
x.y |
Concatenation |
Concatenate two strings |
"MCN"."Solution" |
MCNSolution |
Example
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
$a = 84;
$b = 40;
$c = $a + $b;
echo "Result of Addtion Operator: $c <br/>";
$c = $a - $b;
echo "Result of Substraction Operator: $c <br/>";
$c = $a * $b;
echo "Result of Multiplication Operator: $c <br/>";
$c = $a / $b;
echo "Result of Division Operator: $c <br/>";
$c = $a % $b;
echo "Result of Modulus Operator: $c <br/>";
$c = $a++;
echo "Result of Increment Operator: $c <br/>";
$c = $a--;
echo "Result of Decrement Operator: $c <br/>";
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here