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 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 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.

Operator Name Description Example
+ Addition Add Two Number 10+5
Subraction Subtract second number from first umber 10-5
* Multiplication Multiply Two Number 10*5
/ Division Division of Two Number 10/5
% Modulus Return Remainder after Devision 10%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.

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

Operator Name Description Example
= Assign assign value to variables $num=10
+= Add and assign Assign value to variable after adding value on right hand side in previous value of variable num+=10 ($num = $num+10)
-= Subtract and assign Assign value to variable after subtracting value on right hand side from previous value of variable $num-=10 ($num=$num-10)
*= Multiply and assign Assign value to variable after Multiply value on right hand side with previous value of variable $num*=10 ($num=$num*10)
/= Divide and assign quotient Assign value to variable after dividing previous value of variable with value on right hand $num/=10 ($num=$num/10)
%= Divide and assign modulus Assign 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.

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 relation 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 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:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share




Great News! Now you can read all the tutorials on our Android App..

Download Owlbuddy: Learn Programming Tutorials App Now.

Download Owlbuddy: Learn Programming Tutorials