PHP Operators - Arithmetic operator, Assignment Operator, Increment-decrement Operator
To work with PHP variables and make any operations, PHP operators are used. Operators take one or more values (Operands/Variables) and yields another value. After making, we can call that an expression.
¶Example of Operator and Operands
<?php $summation = 10 + 30; echo $summation; // 40 ?>
In the above example -
- 10 = Operand
- 20 = Operand
Or, this could be also done with Variable -
<?php $number1 = 10; $number2 = 30; $summation = $number2 / $number1; // Here, / is the Division operator echo $summation; // 3 ?>
In this line -
$summation = $number2 / $number1;
$number2 are variables and
/ is the Division operator.
¶Types of Operators
According to operator values it takes, we can distinguish them below types -
- Arithmetic Operator
- Assignment Operator
- Increment-Decrement Operator
- String Operator
- Logical Operator
- Comparison Operator
- Conditional Operator
- Bitwise Operator
- Array Operator
- Type Operator
- Error Control Operator
- Execution Operator.
We’ll discuss only the First three operators in this class. Others will be discussed later, on specific part.
Arithmetic operators are the basic mathematic arithmetics actually. Let’s get the list of arithmetic operators -
¶Examples of Arithmetic Operator
¶Problem 1: Summation of Two number: Sum of 10 and 20
$number1 = 10; $number2 = 20; echo "Summation = " . $number1 + $number2;
¶Problem 2: Division of Two number: 20 divide by 5
$number1 = 20; $number2 = 5; echo "Division = " . $number1 / $number2;
¶Problem 3: Exponentiation of Two number: 5 to the power 3
$number = 5; $power = 3; echo "Exponentiation = " . $number ** $power;
Assignment operators are used to assign value on variable.
<?php $name = "Jhon"; // here = is the assignment operator. ?>
The basic assignment operator is
= (equal to). It actually means that the left operand (
$name) gets set to the value of the expression on the right (
All other assignment operators are actually made from this concept. Let’s get all assignment operators:
|=||Equal to or Assign||
||Assign 5 on
|+=||Plus Equal (Add then Assign)||
|-=||Minus Equal (Subtract then Assign)||
|*=||Multiplication Equal (Multiplication then Assign)||
|/=||Divide Equal (Divide then Assign)||
|%=||Modulo Equal (Modulus then Assign)||
¶Problem 4: Summation and assign
$a = 10; $a += 5; echo "a = " . $a;
Increment or Decrement operators are another kind of PHP operators which used to increase or decrease PHP variables value.
There are two types increment and decrement operator actually.
|Return the value and then increment.|
|Increment the value and Return.|
|Return the value and then decrement.|
|Decrement the value and Return.|
<?php $a = 5; echo $a++; // 5 echo $a; // 6 $b = 20; echo ++$b; // 21 echo $b; // 21 ?>
To manipulate strings or perform any operations on strings, String operator is used.
There are two types string operator actually.
|Concat and append the string with old string.|
|.=||Concatenation and Assign||
|Concat and assign and append the string with old string.|
<?php $name = "Jhon"; echo "Hello " . $name; // Hello Jhon $welcome_text = "Hello "; $name = "Jhon Doe"; $welcome_text .= $name; echo $welcome_text; // Hello Jhon Doe ?>
Echo, PHP Statement, Print, Concat and Comments in PHP
PHP Operators Part 2 - Comparison Operator, Logical Operator, Conditional Operator