PHP – Operators

Operators are used to performs operations in programming. The main operators used in php  programming are given below

  • Arithmetical operators
  • Assignment operators
  • Comparison operators
  • Increment or Decrement operators
  • Logical operators
  • String operators
  • Array operators

 

Arithmetical operators

Arithmetical operators are used in php for finding the result of arithmetical operations. Variables are generally as operands of operation. The main arithmetical operators are described in the table

NameOperatorExampleDescription
Addition+$a+$bSum of $a and $b
Subtraction -$a-$bDifference of $a and $b
Multiplication*$a*$bProduct of $a and $b
Division/$a/$bQuotient of $a and $b
Modulo%$a%$bReminder of $a and $b
Exponentiation**$a**$b$b'th power of $a or $a multiply $b times

Example

Output

 

Assignment operators

Assignment operators are used to assigning the value of one variable to other.   =    is the main assignment operator used in php. Here the value of right variable is assigned to variable present in the left side of equal to.

OperatorExampleMeaning of exampleDescription
=a=ba=b'a' get the value of 'b'
+=a+=ba=a+bAddition
-=a-=ba=a-bSubtraction
*=a*=ba=a*bMultiplication
/=a/=ba=a/bDivision
%=a%=ba=a%bModulus

 

Example 1

Output

 

Example 2

Output

 

Example 3

Output

 

Example 4

Output

 

Example 5

Output

 

Example 6

Output

 

Comparison operators

Comparison operators are used to compare two values in programming.

NameOperator ExampleDescription
Equal ==$a==$bTrue value returns when variable 'a' equal to 'b'
Identical===$a===$bTrue value return when variable 'a' identical 'b'
Less than <$a<$bTrue value returns when variable 'a' less than 'b'
Greater than>$a>$bTrue value returns when variable 'a' greater than 'b'
Less than Or equal <=$a<=$bTrue value returns when variable 'a' less than 'b' or 'a' equal to 'b'
Greater than or equal >=$a>=$bTrue value returns when variable 'a' greater than 'b' or 'a' equal to 'b'
Not equal !=$a!=$bTrue value returns when variable 'a' not equal to 'b'
<>$a<>$b
Not identical!==$a!==$bTrue value returns when variable 'a' is not identical to 'b'

 

Example 1

Output

 

Example 2

Output

 

Example 3

Output

 

Example 4

Output

 

Example 5

Output

 

Example 6

Output

 

Example 7

Output

 

Example 8

Output

 

Increment or Decrement Operators

Increment operators are used to increment the value of variable in php and decrement operators are used to decrement value of of variable in php.

Name ExampleDescription
Pre-increment++$aReturn $a after increments $a by one
Post-increment$a++Return $a before increments $a by one
Pre-decrement--$aReturn $a after decrements $a by one
Post-decrement$a--Return $a before decrements $a by one

 

Example 1

Output

 

Example 2

Output

 

Example 3

Output

 

Example 4

Output

 

Logical operators

Logical operators are used to combine two or more conditions in programming.

NameOperatorExampleDescription
Andand$a and $bTrue value returns only both a and b are true
&&$a && $b
Oror$a or $bTrue value return if either a or b is true
||$a || $b
Not!!$aTrue value return only if a is not true
Xorxor$a xor $bTrue value returns if either a or b is true, but not both

 

Example 1

Output

 

Example 2

Output

 

Example 3

Output

 

Example 4

Output

 

String operators

String operators are used to perform string operations in php.

NameOperatorExampleDescription
Concatenation.$a . $bConcatenate of $a and $b
Concatenation Assignment.=$a .= $b$b appends to $a

 

Example 1

Output

 

Example 2

Output

 

Array operators

Array operators are used to perform array comparison operations in php.

NameOperatorExampleDescription
Equality==$a==$bReturns true only if both a and b have same key/value pairs
Identity===$a===$bReturns true only if both a and b have Same key/value pairs and also same order type
Union+$a+$bUnion of a and b
Inequality!=$a!=$bReturns true only if a and b are not equal
<>$a<>$b
Non identity!==$a!==$bReturns true value only if a and b are not identical

 

Example 1 [ Equality ]

Output

 

Example 2  [ Identity ]

Output

 

Example 3  [ Identity ]

Output

 

Example 4  [ Union ]

Output

 

Example 5  [ Union ]

Output

 

Note: The union operator adds arrays which have unique keys. If  the arrays have same keys this will neglect the one array keys with the order.

 

Example 6  [ Inequality ]

Output

 

Example 7  [ Inequality ]

Output

 

Example 8  [ Non-Identity ]

Output

To See the Difference between   !=  and !==   check  Example 8  [ Non-Identity ]  and   Example 6  [ Inequality ]

Leave a Reply