Operators in PHP

Operators are special symbols that tell to PHP processor to do some special operations. For example, there is a + operator which tells PHP Processor to add two numbers (4+5). There are various operators available 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 they work.

Arithmetic Operators: –

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

Operator Name Description Example
+ Addition Add Two Number 10+5
Subtraction Subtract the second number from the first number 10-5
* Multiplication Multiply Two Number 10*5
/ Division Division of Two Number 10/5
% Modulus Return remainder after Division 10%5

Comparison Operators: –

Comparisons operators are used to compare two numbers. For example to check which number is greater than the given numbers (10>5). The result of comparison operators comes in Boolean form mean 10>5 result of this would be true. The list of operators in this category is as follows.

Operator Name Description Example
== Equal Return true if the first value would equal to second. 10==10
=== Identical Return true if both operands would be of the same data type. 10===10
!= Not equal Return true if both values are not the same. 10!=5
!== Not identical Return true if both values would not be of the same type. 10!===5
< Less than Return true if the first value will be less than second 10<20
> Greater than Return true if the first value will greater than second 10>5
<= Less than or equal to Return true if the first value will be less than or equal to second 10<=5
>= Greater than or equal to Return true if the first value will be greater than or equal to second 10>=5

Assignment Operators: –

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

Operator Name Description Example
= Assign assign value to variables $num=10
+= Add and assign Assign a value to the variable after adding a value on the right-hand side in the previous value of the variable. num+=10 ($num = $num+10)
-= Subtract and assign Assign a value to the variable after subtracting the value on the right-hand side from the previous value of the variable. $num-=10 ($num=$num-10)
*= Multiply and assign Assign a value to the variable after multiplying the value on the right-hand side with the previous value of the variable. $num*=10 ($num=$num*10)
/= Divide and assign quotient Assign a value to the variable after dividing the previous value of the variable with the value on the right hand. $num/=10 ($num=$num/10)
%= Divide and assign modulus Assign the remainder to the variable after dividing the previous value of the variable with the value on the right hand $num%=10 ($num=$num%10)

Inc/Dec Operators: –

Increment and Decrements are unary operators means it only works with a 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.

Operator Name Description Example
++ Increment Increment 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 relations between operands. These operators are used to perform bit-level operations.

Operator Name Description Example
and And Return TRUE if both $op1 and $op2 are true $op1 and $op2
Or Or Return TRUE if either $op1 or $op2 is true $op1 or $op2
xor Xor Return TRUE if either $op1 or $op2 is true but not both $op1 xor $op2
! Not Return TRUE if $op1 is not true ! $op1
&& And Return TRUE if either $op1 and $op2 are true $op1 && $op2
|| Or Return TRUE if either $op1 or $op2 is true $op1 || $op2

String Operators: –

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

Operator Name Description Example
. Concatenation Concatenate $str1 with $str2 $str1 . $str2
.= Concatenation and Assignment First 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.

Operator Name Description Example
+ Union Union of $num1 and $num2 $num1 + $num2
== Equality Return TRUE if $num1 and $num2 have same key/value pair $num1 == $num2
!= Inequality Return TRUE if $num1 is not equal to $num2 $num1 != $num2
=== Identity Return TRUE if $num1 and $num2 have the same key/value pair of the same type in the same order $num1 === $num2
!== Non-Identity Return TRUE if $num1 is not identical to $num2 $num1 !== $num2
<> Inequality Return TRUE if $num1 is not equal to $num2 $a <> $b
Spread the love
Scroll to Top
×