Operator PHP

What is Operator in PHP ?

Operator in PHP : An operator is a symbol that tells  to perform specific mathematical or logical functions. In PHP there  are following types of operators

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical (or Relational) Operators
  4. Assignment Operators
  5. Conditional (or ternary) Operators

Arithmetic Operators in PHP:

Arithmetic Operators are those operators which perform Calculation .If we want to perform Mathematical operation then  Arithmetic Operators are use  like +,-,*,/,%,++,–. Generally it is used for calculation between variables.

Assume $x and $y  are two variables having value $x=10 ,$y=5, Then the Result will be give by Operators are

Operator Example Result Operation Performed
+ $x + $y 30 Adds two operands
$x- $y 5 Subtracts second operand from the first
* $x* $y 50 Multiply both operands
/ $x / $y 2 Divide numerator by de-numerator
% $x % $y 0 Modulus Operator and remainder of after an integer division
++ $x++ 11 Increment operator, increases integer value by one
$x– 9 Decrement operator, decreases integer value by ones

Comparison Operators in PHP:

Comparison Operators are those operators which perform Comparison .If we want to perform any Comparison operation then Comparison Operators are use  like ==,!=,<,>,>=,<=. Generally it is used for Comparison between variables or for to check whether the desired value is available or not.

Assume $x and $y  are two variables having value $x=10 ,$y=5, Then the Result will be give by Operators are

Operator Example Result Operation Performed 
== ($x == $y) False If value of two operands are equal or not, if yes then condition becomes true.
!= ($x != $y) True If value of two operands are equal or not, if values are not equal then condition becomes true.
> ($x  > $y) True If value of left operand is greater than value of right operand, if yes then condition becomes true.
< ($x  < $y) False If value of left operand is less than value of right operand, if yes then condition becomes true.
>= ($x  >= $y) True If value of left operand is greater than or equal to value of right operand, if yes then condition becomes true.
<= ($x  <= $y) False If value of left operand is less than or equal to value of right operand, if yes then condition becomes true.
 ===  ($x===$y)  False  If value of two operands are equal and the data types are same , if yes then condition becomes true.

Logical  Operators in PHP:

Logical  Operators is also called Relational Operators. These operator Checks the logic of the condition where the condition goes right or wrong.For example we want to check that give value is according to us then perform first Condition if not then Second condition
Assume $x and $y  are two  variables having value $x=10 ,$y=5, Then the Result will be give by Operators are

Operator Example Name of Logical Operator Operation Performed 
and ($x and $y) is true. AND operator. If both the operands are true then condition becomes true.
or ($x or $y) is true. OR Operator. If any of the two operands are non zero then condition becomes true.
! !($x && $y) is false. NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
&& ($x && $y) is true. AND operator. If both the operands are non zero then condition becomes true.
|| ($x || $y) is true. OR Operator. If any of the two operands are non zero then condition becomes true.

 

 

 

 

Assignment Operators in PHP:

Assignment Operators are those operator which perform assignment of value in variable according to operator.In  php there are many Assignment Operators .Don\’t get Confuse between \”=\” and \”==\”.in PHP \”=\” is assignment operator where as \”==\” is Comparison operator 

Operator Example Working As Operation Performed
= $z = $x + $y assign value of $x + $y into $z Simple assignment operator, Assigns values from right side operands to left side operand
+= $z += $x $z = $z + $x Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
-= $z -= $x $z = $z -$x Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
*= $z *= $x $z = $z * $x Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
/= $z /= $x $z = $z / $x Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
%= $z %= $x $z = $z % $x Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

Conditional Operator in PHP:

Conditional Operator is sometimes also called ternary operator.In ternary Operator  first Checks an expression for a true or false value and then perform given statements depending upon the result of the evaluation.

Assume $x and $y  are two  variables having value $x=10 ,$y=5, Then the Result will be give by Operators are

 

Operator Example Result Operration Performed
? :  (($x < $y ) ? $z=$x :$z=$y); $z=5 If Condition is true ? Then value $x : Otherwise value $y
Scroll to Top