PHP Menu


Operators in PHP


An operator can be defined as a symbol which is used to perform a particular operation between operands.

PHP provides a variety of operators described as follows.

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

☞ Arithmetic operators

Arithmetic operators are used to perform arithmetic operations between two operands.

Operator Description
+ (addition) Add two operands
- (subtraction) Subtract right operand from the left
*(multiplication) Multiply two operands
/ (divide) Divide left operand by the right one (always results into float)
%( reminder) Modulus - remainder of the division of left operand by the right
** (exponent) Exponent - left operand raised to the power of right

☞ Assignment Operators

The assignment operators are used to assign the value of the right expression to the left operand.

Operator Description
= (Assigns to) Assigns values from right side operands to left side operand.
+= (Assignment after Addition) It adds right operand to the left operand and assign the result to left operand.
-= (Assignment after Subtraction) It subtracts right operand from the left operand and assign the result to left operand.
*= (Assignment after Multiplication) It multiplies right operand with the left operand and assign the result to left operand.
/= (Assignment after Division) It divides left operand with the right operand and assign the result to left operand.
%= (Assignment after Modulus) It takes modulus using two operands and assign the result to left operand.
**= (Assignment after Exponent) Performs exponential (power) calculation on operators and assign value to the left operand.

☞ Comparison operators

Comparison operators are used to comparing the value of the two operands and returns boolean true or false accordingly.

Operator Description
== (Equal to) Equal to - True if both operands are equal.
!= (Not equal to) Not equal to - True if operands are not equal.
<= (Less than or equal) Less than or equal to - True if left operand is less than or equal to the right
>= (Greater than or equal) Greater than or equal to - True if left operand is greater than or equal to the right
< (Less than) Less than - True if left operand is less than the right
> (Greater than) Greater than - True if left operand is greater than the right
=== (Indentical) Indentical - True if both operands have same value and datatype.
!== (Not Indentical) Not Indentical - True if both operands doesn't have same value and datatype..

☞ Increment/Decrement operators

Increment/Decrement operators are used to increment or decrement the value of the operand.

Operator Description
++ (Increment) Increment - It increments the value of operand by one.
-- (Decrement) Decrement - It decrements the value of operand by one.

☞ Logical Operators

The logical operators are used primarily in the expression evaluation to make a decision.

Operator Description
and (or) && (logical and) Returns True if both statements are true
or (or) || (logical or) Returns True if one of the statements is true
not (or) ! (logical not) Reverse the result, returns False if the result is true

☞ String operators

String operators are used to concatenate two strings.

Operator Description
. (Concatenation) It concatenates left operand and right operand.
.= (Assignment after Concatenation) It concatenates left operand and right operand and assign the result to left operand.

☞ Array operators

Array operators are used to perform various operations on arrays.

Operator Description
+ (Union) It combine two arrys.
== (Equal to) Equal to - True if both operands(arrays) are equal.
!= (Not equal to) Not equal to - True if operands(arrays) are not equal.
=== (Indentical) Indentical - True if both operands(arrays) have same value and datatype.
!== (Not Indentical) Not Indentical - True if both operands(aarays) doesn't have same value and datatype..

Example: "OperatorDemo.php"


<html>

<head>
<title>Operators Demo</title>
</head>
<body>
<?php
$a
=10;
$b=5;
//Arithmetic operators
$c=$a+$b;
echo 
"Addition is :$c <br/>";
$c=$a-$b;
echo 
"Subtraction is :$c <br/>";

//Increment/Decrement Operators
$c=$a++;
echo 
"Increment is :$c <br/>";
$c=$a--;
echo 
"Decrement is :$c <br/>";

//Assignment Operators
$c+=$a;
echo 
" Add Assignment is :$c <br/>";
$c-=$a;
echo 
"Sub Assignment is :$c <br/>";

//Comparison operators
echo "$a > $b  is : ".($a>$b)."<br/>";
echo 
"$a < $b  is : ".($a<$b)."<br/>";

//String operators
$str1="Study";
$str2="Glance";
$str3=$str1.$str2;
echo 
"Combined String : $str3 <br>";
 
//Logical Operators
echo "$a > $b and $a < $c   is : ".($a>$b && $a<$c)."<br/>";
echo 
"$a > $b or $a < $b   is : ".($a>$b || $a<$b)."<br/>";
?>
</body>
</html>

Output :

image

Next Topic :Control Structures in PHP