Operators in php

This is one of the most important topic which we are going to learn today so do not distract while learning the totorial, you may put your phone on silent mode so that gf may not disturb you right now (just fun). Let’s start :  as we know that ALPHABETS are the basics to write a paragraph, in the same manner Operators are used in php. Senior experienced developers also commits mistakes while using this. Even in interview the tricky/confusing questions are asked using operators. So i am giving you a simple hint to pay extra attention on this topic and if there is any doubt, you may let us know by dropping your comments.

What are operators ?

Operators are the thing which is used to perform some operation on data, based on the type of operator we are using. In php we use several types of operator as mentioned below.

Types of operator in php

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

Now we will study in details about each type of data type. so first we will start with Arithmetic operators.

Arithmetic Operators

PHP arithmetic operators are those operators which are used to perform addition, subtraction, multiplication, division, modulus e.t.c

Arithmetic Operator Name Description
+ Addition ($p + $q) ,  Sum of $p and $q
Substraction ($p – $q) ,  Substraction of $p and $q
* Multiplication ($p * $q) ,  Multiplication of $p and $q
/ Division ($p / $q) ,  Division of $p and $q
% Modulus ($p % $q) ,  Remainder of $p divided by $q
** Exponentiation ($p ** $q) ,  $q is exponent of $p (from php 5.6)

Lets see an example of php arithmetic operator.

PHP Assignment operators

PHP assignment operators are used to assign/write the value to the variable. In PHP the assignment operator is denoted by ” = ” . Which says that the value or expression on the right side of  = is assigned to the left operand i.e variable.

Assignment Operator is same as Description
$p = $q $p = $q Value of $q is assigned to $p
$p += $q $p = $p + $q Addition of $p, $q assigned to  $p
$p -= $q $p = $p – $q Difference of $p and $q assigned to $p
$p *= $q $p = $p * $q Product of $p and $q assigned to $p
$p /= $q $p = $p / $q Division of $p by $q assigned to $p
$p % $q $p = $p % $q Modulus of $p , $q assigned to $p

let’s take an example of php assignment operator.

PHP Comparison operators

PHP Comparison operators are those operators which is used to compare two values. These are basic operators which you might know but go through once to clear the concepts. There are several types of PHP comparison operators which i am listing down.

Comparison Operator Name Description
== Equal ($p == $q), returns true if $p is equal to $q.
=== Identical ($p === $q), returns true if $p is equal to $q, and $p and $q are of same type.
!= Not equal ($p != $q), returns true if $p is not equal to $q.
<> Not equal ($p <> $q), returns true if $p is not equal to $q.
!== Not identical ($p !== $q), returns true if $p is not equal to $q or $p and $p are not of same type
> Greater than ($p > $q), returns true if $p is greater than $q.
< Less than ($p < $q), returns true if $p is less than $q.
>= Greater than or equal to ($p >= $q), return true if $p is greater than or equal to $q
<= Less than or equal to ($p <= $q), return true if $p is less than or equal to $q

let’s see an example of php comparison operator below.

PHP Increment and Decrement Operators

As we know that, Increment means to increase and decrement means decrease. While dealing with PHP operators its meaning is same here also. Increment and decrement operators are used to increase or decrease the value of the variable. Lets see how it happens.

Increment/Decrement Operator Name Description
$i++ Post Increment Return $i then increment $i by one.
++$i Pre Increment Increment $i by one and then return $i
$i– Post Decrement Return $i and then decrement $i by one.
–$i Post Decrement Decrement $i by one and then return $i

You have seen we used two names (i.e post and pre increment/decrement). A question arises now arise is : What is post and pre increment/decrement in php ?.

Lets understand this through by examples.

PHP Logical Operators

Logical operators are generally used in conditional statement. Whenever you require some output based on some condition then we use logical operators.

Lets see how it works

 

Leave a Reply

Your email address will not be published. Required fields are marked *