Operator Types
ETL LABS PVT LTD – PHP 47
Operator Types
1 2 3
Arithmetic Operators Comparison Operators Logical (or Relational) Operators
4 5 6
Assignment Operators Conditional (or ternary) Operators String Operators
ETL LABS PVT LTD – PHP 48
Arithmetic Operators
Operator Name Syntax Operation
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
** Exponentiation $x ** $y Result of raising $x to the $y'th power
% Modulus $x % $y Remainder of $x divided by $y
ETL LABS PVT LTD – PHP 49
Comparison Operators
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
ETL LABS PVT LTD – PHP 50
Comparison Operators
Operator Name Example Result
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
Greater than
>= $x >= $y Returns true if $x is greater than or equal to $y
or equal to
Less than or
<= $x <= $y Returns true if $x is less than or equal to $y
equal to
ETL LABS PVT LTD – PHP 51
Logical Operators
Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
ETL LABS PVT LTD – PHP 52
Assignment Operators
Operator Name Example Result
Operand on the left obtains the value of the
= Assign $x = $y
operand on right
+= Add then Assign $x += $y Simple Addition same as $x = $x + $y
-= Subtract then Assign $x -= $y Simple subtraction same as $x = $x – $y
*= Multiply then Assign $x *= $y Simple product same as $x = $x * $y
Divide then Assign
/= $x /= $y Simple division same as $x = $x / $y
(quotient)
Divide then Assign
%= %= Simple division same as $x = $x % $y
(remainder)
ETL LABS PVT LTD – PHP 53
Conditional or Ternary Operator
These operators are used to
Operator Name Result compare two values and take either
of the result simultaneously,
If condition is true ? then $x : or depending on whether the outcome
else $y. This means that if is TRUE or FALSE. These are also
?: Ternary condition is true then left result of used as shorthand notation for
the colon is accepted otherwise if…else statement that we will read
the result on right. in the article on decision making.
Syntax:
$var = (condition)? value1 : value2;
ETL LABS PVT LTD – PHP 54
String Operators
Operator Name Example Result
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
Concatenation
.= $txt1 .= $txt2 Appends $txt2 to $txt1
assignment
ETL LABS PVT LTD – PHP 55