Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

In PHP, Operators are special symbols that are used to perform operations on variables and values.

PHP has various types of operators-

  1. Arithmetic operator
  2. Comparison operator
  3. Logical operator
  4. Assignment operator
  5. Increment and Decrement operator
  6. Bitwise operator

Arithmetic operator in PHP:

Arithmetic operators Example like: +, -, *, /, % (modulus), ** (exponentiation)

OperatorNameExampleExplanation
+Addition$a + $bSum of operands
Subtraction$a – $bDifference of operands
*Multiplication$a * $bProduct of operands
/Division$a / $bQuotient of operands
%Modulus$a % $bRemainder of operands
**Exponentiation$a ** $b$a raised to the power $b

Assignment operator in PHP:

Assignment operators: =, +=, -=, *=, /=, %=, .= (concatenation assignment)

OperatorNameExampleExplanation
=Assign$a = $bThe value of right operand is assigned to the left operand.
+=Add then Assign$a += $bAddition same as $a = $a + $b
-=Subtract then Assign$a -= $bSubtraction same as $a = $a – $b
*=Multiply then Assign$a *= $bMultiplication same as $a = $a * $b
/=Divide then Assign
(quotient)
$a /= $bFind quotient same as $a = $a / $b
%=Divide then Assign
(remainder)
$a %= $bFind remainder same as $a = $a % $b

Comparison operators:

Comparison operators: ==, === (identical), !=, !== (not identical), <, >, <=, >=

OperatorNameExampleExplanation
==Equal$a == $bReturn TRUE if $a is equal to $b
===Identical$a === $bReturn TRUE if $a is equal to $b, and they are of same data type
!==Not identical$a !== $bReturn TRUE if $a is not equal to $b, and they are not of same data type
!=Not equal$a != $bReturn TRUE if $a is not equal to $b
<>Not equal$a <> $bReturn TRUE if $a is not equal to $b
<Less than$a < $bReturn TRUE if $a is less than $b
>Greater than$a > $bReturn TRUE if $a is greater than $b
<=Less than or equal to$a <= $bReturn TRUE if $a is less than or equal $b
>=Greater than or equal to$a >= $bReturn TRUE if $a is greater than or equal $b
<=>Spaceship$a <=>$bReturn -1 if $a is less than $b
Return 0 if $a is equal $b
Return 1 if $a is greater than $b

Logical operator:

Logical operators: && (and), || (or), ! (not)

OperatorNameExampleExplanation
andAnd$a and $bReturn TRUE if both $a and $b are true
OrOr$a or $bReturn TRUE if either $a or $b is true
xorXor$a xor $bReturn TRUE if either $ or $b is true but not both
!Not! $aReturn TRUE if $a is not true
&&And$a && $bReturn TRUE if either $a and $b are true
||Or$a || $bReturn TRUE if either $a or $b is true

Increment and Decrement operator:

Increment/Decrement operators: ++ (increment), — (decrement)

NameOperator
Pre-increment++$x
Post-Increment$x- –    
Pre-decrement–$x
Post-decrement$x- –

Bitwise operator:

Bitwise operators: & (and), | (or), ^ (xor), ~ (not), << (left shift), >> (right shift)

OperatorNameExampleExplanation
&And$a & $bBits that are 1 in both $a and $b are set to 1, otherwise 0.
|Or (Inclusive or)$a | $bBits that are 1 in either $a or $b are set to 1
^Xor (Exclusive or)$a ^ $bBits that are 1 in either $a or $b are set to 0.
~Not~$aBits that are 1 set to 0 and bits that are 0 are set to 1
<<Shift left$a << $bLeft shift the bits of operand $a $b steps
>>Shift right$a >> $bRight shift the bits of $a operand by $b number of places

Related Posts

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Artificial Intelligence