Operators in PHP

In this tutorial, we will lean Operators in PHP. Operators are special symbols which tell to PHP processor to do some special operations. For example, There is + operator which tells PHP Processor to add two numbers (4+5). There are various operators in PHP and they are divided into some categories. List of Categories:

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Inc/Dec Operators
  • Logical Operators
  • String Operators
  • Array Operators

Now we will learn about each category in detail. We will learn which operators are in each category and how do they work.

Arithmetic Operators:

There is a total of five operators in this category. Arithmetic operators are used to performing some basic mathematical operations. Operators in this category are +,-, /, *, %. Checklist of Arithmetic Operators.

OperatorNameDescriptionExample
+AdditionAdd Two Number10+5
SubractionSubtract second number from first umber10-5
*MultiplicationMultiply Two Number10*5
/DivisionDivision of Two Number10/5
%ModulusReturn Remainder after Devision10%5

Comparison Operators: 

Comparisons operators are used to comparing two numbers. For example to check which number is greater from giver number(10>5). Result of comparison operators come in Boolean form mean 10>5 result of this would be true. List of operators in this category is as follow.

OperatorNameDescriptionExample
==EqualReturn true if first value would equal to second10==10
===IdenticalReturn true if both operands would be of same data type10===10
!=Not equalReturn true if both values are not same10!=5
!==Not identicalReturn true if both values would not be of same type.10!===5
<Less thanReturn true if first value will be less than second10<20
>Greater thanReturn true if first value will greater than second10>5
<=Less than or equal toReturn true if first value will be less than or equal to second10<=5
>=Greater than or equal toReturn true if first value will be greater than or equals to second10>=5

Assignment Operators:

The most common Assignment operator is =. Assignment operators are used to assigning value to variables. There is a total of six assignment operators check this list.

OperatorNameDescriptionExample
=Assignassign value to variables$num=10
+=Add and assignAssign value to variable after adding value on right hand side in previous value of variablenum+=10 ($num = $num+10)
-=Subtract and assignAssign value to variable after subtracting value on right hand side from previous value of variable$num-=10 ($num=$num-10)
*=Multiply and assignAssign value to variable after Multiply value on right hand side with previous value of variable$num*=10 ($num=$num*10)
/=Divide and assign quotientAssign value to variable after dividing previous value of variable with value on right hand$num/=10 ($num=$num/10)
%=Divide and assign modulusAssign remainder to variable after dividing previous value of variable with value on right hand$num%=10 ($num=$num%10)

Inc/Dec Operators:

Increment and Decrements are unary operators means it only works with the single operand. These operators are used to increase or decrease the value of a variable by one. Both Inc and Dec come in two types pre-increment and post-increment.

OperatorNameDescriptionExample
++IncrementIncrement the value of $a by one, then return $a

Return $a, then increment the value of $a by one
++$a


$a++
Decrement


Decrement the value of $a by one, then return $a

Return $a, then Decrement the value of $a by one
–$a


$a–

Logical Operators:

Logical Operators are used to check logical relation between operands. These operators are used to perform bit level operations.

OperatorNameDescriptionExample
andAndReturn TRUE if both $op1 and $op2 are true$op1 and $op2
OrOrReturn TRUE if either $op1 or $op2 is true$op1 or $op2
xorXorReturn TRUE if either $op1 or $op2 is true but not both$op1 xor $op2
!NotReturn TRUE if $op1 is not true! $op1
&&AndReturn TRUE if either $op1 and $op2 are true$op1 && $op2
||OrReturn TRUE if either $op1 or $op2 is true$op1 || $op2

String Operators:

String operators are used to perform some special operations with Strings.

OperatorNameDescriptionExample
.ConcatenationConcatenate $str1 with $str2$str1 . $str2
.=Concatenation and AssignmentFirst will concatenate $str1 with $str2, then assign the final concatenated string to $str1,$str1 .= $str2

Array Operators:

Array operators are used to perform special operations with Arrays.

OperatorNameDescriptionExample
+UnionUnion of $num1 and $num2$num1 + $num2
==EqualityReturn TRUE if $num1 and $num2 have same key/value pair$num1 == $num2
!=InequalityReturn TRUE if $num1 is not equal to $num2$num1 != $num2
===IdentityReturn TRUE if $num1 and $num2 have same key/value pair of the same type in the same order$num1 === $num2
!==Non-IdentityReturn TRUE if $num1 is not identical to $num2$num1 !== $num2
<>InequalityReturn TRUE if $num1 is not equal to $num2$a <> $b
Spread the love