Arithmetic Operators in php Examples
In this topic of Arithmetic Operators in php Examples , we will list arithmetic operators available in PHP. Like other programming languages, PHP also have arithmetic operators to perform various arithmetic operations like addition, subtraction, division , multiplication and exponentiation. We have one new operator in php which we do not have in C and C++, i.e. exponentiation operator.
Lets us explore all the PHP arithmetic operators one by one.
Operator for Addition in PHP
To perform addition operation we need plus operator. The operator for addition is ( + ) sign.
Suppose you wants to add two variables say A and B and result of the addition is to be stored in variable C.
$C=$A+$B
Operator for Subtraction in PHP
To perform subtraction operation we need minus operator. The operator for subtraction is ( - ) sign.
Suppose you wants to subtract value of variable A from variable B and result of the subtraction is to be stored in variable C.
$C=$B-$A
Operator for Multiplication
To perform multiply operation we need multiplication operator. The operator for multiplication is ( * ) sign.
Suppose you wants to multiply value of variable A with value of variable B and result of the multiplication is to be stored in variable C.
$C=$A*$B
Operator for Division in PHP
To divide value of a variable by value of some other variable we need division operator . The operator for division is ( / ) sign.
Suppose you wants to divide value of variable A by the value of variable B and result of the division is to be stored in variable C.
$C=$A/$B
Modulus Operator of PHP
Modulus operator of PHP is (%).
When a number is divided by another number , the modulus operator returns remainder of this division.
Exponentiation Operator of PHP
The exponentiation operator of PHP is (**).
Suppose you supply $A=3**2;
After the execution of the above statement , A will have value 9.