In this lesson, you will learn about operations in PHP, in particular the arithmetic operations, and their usage, along with examples to better understand the topic.
Like in general mathematics, operators are utilized for performing operations on variables. Operations on operands are performed using the PHP Operator symbol. The variable operations are performed with the help of operators. Consider this:
$numVar = 524 + 400;// + is the operator and 524,400 are operands
On behalf of operands, we can also classify operators. The binary + operator, the operands (524 and 400), and the variable $numVar
are all used in the example above. They fall into one of the following categories
Operating on a single operand, unary operators include ++
, --
, and so forth.
Binary operators, which work with two operands, include binary or, and(&), Or (|), Xor (^)
Operate on three operands using a ternary operator, such as “?:”.
The following PHP arithmetic operators are given below and used for numeric values to perform arithmetic operations.
Arithmetic Operators Symbol | Arithmetic Operators’ Names | Example |
---|---|---|
+ | Addition | a + b |
– | Substraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
** | Exponentiation | a ** b |
++ | Increment | a++ |
— | Decrement | a– |
Example
Example <?PHP echo "PHP Arithmetic Operators Example"; echo "<br>"; echo " ---------------------------------------- "; echo "<br>"; $a = 25; $b = 20; echo '$a = 25'; echo "<br>"; echo '$b = 20'; echo "<br>"; $c = + $a; echo "Identity Operator-> ", '+$a =', $c; echo "<br>"; $c = - $a; echo "Negation Operator-> ", '-$a =', $c; echo "<br>"; $c = $a + $b; echo "Add Operator-> ", '$a + $b =', $c; echo "<br>"; $c = $a - $b; echo "Substract Operator -> ", '$a - $b =', $c; echo "<br>"; $c = $a * $b; echo "Multiply Operator -> ", '$a * $b =', $c; echo "<br>"; // quotient of $a and $b. Moreover, the division operator displays a floating value except in the case when two operands are integers and these integers are consistently divisible with each other. $c = $a / $b; echo "Divide Operator -> ", '$a / $b =', $c; //The remainder of $a divided by $b. Moreover, prior to processing of the expression, the operands of modulus are also changed by shedding the decimal part. The modulo operator % displays the same sign of result as the dividend. $c = $a % $b; echo "<br>"; echo "Modulus Operator -> ", '$a % $b =', $c; $c = $a % -$b; echo "<br>"; echo "Modulus Operator -> ", '$a % -$b =', $c; $c = - $a % $b; echo "<br>"; echo "Modulus Operator -> ", '-$a % $b =', $c; $c = - $a % -$b; echo "<br>"; echo "Modulus Operator -> ", '-$a % -$b =', $c; $c = $a**$b; echo "<br>"; echo "Exponent Operator -> ", '$a ** $b =', $c; $c = $a++; echo "<br>"; echo "Increment Operation -> ", '$a++ =', $c; echo "<br>"; $c = $a--; echo "Decrement Operation -> ", '$a -- =', $c; echo "<br>"; ?>
Output
PHP Arithmetic Operators Example ---------------------------------------- $a = 25 $b = 20 Identity Operator-> +$a =25 Negation Operator-> -$a =-25 Add Operator-> $a + $b =45 Substract Operator -> $a - $b =5 Multiply Operator -> $a * $b =500 Divide Operator -> $a / $b =1.25 Modulus Operator -> $a % $b =5 Modulus Operator -> $a % -$b =5 Modulus Operator -> -$a % $b =-5 Modulus Operator -> -$a % -$b =-5 Exponent Operator -> $a ** $b =9.0949470177293E+27 Increment Operation -> $a++ =25 Decrement Operation -> $a -- =26
This concludes the Arithmetic Operators in PHP lesson. In the next lesson, you will study another type of operation in PHP programming, the Comparision Operators.