Operators in PHP are used to perform different operation and are divided into following types.

- Arithmetic Operators
- String Operators
- Assignment Operators
- Comparison Operators
- Bitwise Operators
- Logical Operators

Following arithematic operators supported by PHP.

Assume variable A holds 5 and variable B holds 10 then

Operator | Description | Example |
---|---|---|

+ | Adds two operands | A + B will give 15 |

- | Subtracts second operand from the first | A - B will give -5 |

* | Multiply both operands | A * B will give 50 |

/ | Divide numerator by denumerator | B / A will give 2 |

% | Modulus Operator and remainder of after an integer division | B % A will give 0 |

++ | Increment operator, increases integer value by one | A++ will give 6 |

-- | Decrement operator, decreases integer value by one | A-- will give 4 |

In addition to the comparison operators, which can be used on string values, PHP supports two String Operators:

First, the concatenation operator (.) which concatenates two string values together, returning another string that is the union of the two operand strings. For example, "my " . "string" returns the string "my string".

Second, The concatenation assignment operator .= which can also be used to concatenate strings. For example, if the variable firstName has the value "Charlie", then the expression firstName .= " Write" evaluates to "Charlie Write" and assigns this value to firstName.

In PHP, an assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.

Following assignment operators supported by PHP.

Assume variable X holds 5 and variable Y holds 10 then:

Operator | Description | Example |
---|---|---|

= | It assigns values from right side operands to left side operand | Z = X + Y will assigne value of X + Y into Z |

+= | It adds right operand to the left operand and assign the result to left operand. | Z += X is equivalent to Z = Z + X |

-= | It subtracts right operand from the left operand and assign the result to left operand. | Z -= X is equivalent to Z = Z - X |

*= | It multiplies right operand with the left operand and assign the result to left operand. | Z *= X is equivalent to Z = Z * X |

/= | It divides left operand with the right operand and assign the result to left operand. | Z /= X is equivalent to Z = Z / X |

%= | It takes modulus using two operands and assign the result to left operand. | Z %= X is equivalent to Z = Z % X |

In PHP a comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be string,numerical, logical, or object values. Strings are compared on lexicographical ordering, using Unicode values. In all cases, if the two operands are not of the same type, PHP attempts to convert them to an appropriate type for the comparison.

Following comparison operators supported by PHP.

Assume variable A holds 5 and variable B holds 10 then:

Operator | Description | Example |
---|---|---|

== | Checks if the value of two operands are equal or not. | (A == B) is not true. |

!= | Checks if the value of two operands are equal or not. | (A != B) is true. |

> | Checks if the value of left operand is greater than the value of right operand. | (A > B) is not true. |

< | Checks if the value of left operand is less than the value of right operand. | (A < B) is true. |

>= | Checks if the value of left operand is greater than or equal to the value of right operand. | (A >= B) is not true. |

<= | Checks if the value of left operand is less than or equal to the value of right operand. | (A <= B) is true. |

In PHP bitwise operators work with 32-bit integers.Bitwise operators perform an operation on the bit (0 or 1).

Following bitwise operators supported by PHP.

Assume variable X holds 5 and variable Y holds 10 then:

Operator | Description | Example |
---|---|---|

Bitwise AND | It performs a Boolean AND operation on each bit of its integer arguments. | (X & Y) is 2. |

Bitwise OR | It performs a Boolean OR operation on each bit of its integer arguments. | (X | Y) is 3. |

Bitwise XOR | It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. | (X ^ Y) is 1. |

Bitwise NOT | It is a is a unary operator and operates by reversing all bits in the operand. | (~Y) is -4 . |

Left shift | It moves all bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. | (X << 1) is 4. |

Right shift | It moves all bits in its first operand to the right by the number of places specified in the second operand. The bits filled in on the left depend on the sign bit of the original operand, in order to preserve the sign of the result. | (X >> 1) is 1. |

In PHP logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value.

Following logical operators supported by PHP.

Assume variable X holds 5 and variable Y holds 10 then:

Operator | Description | Example |
---|---|---|

&& | Known as Logical AND operator. If both the operands are true then then condition becomes true. | (X && Y) is true. |

|| | Known as Logical OR Operator. If any of the two operands is true then then condition becomes true. | (X || Y) is true. |

! | Known as Logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(X && Y) is false. |

and | Known as Logical AND operator. If the value of both the operands is true then condition becomes true. | (X and Y) is true. |

or | Known as Logical OR operator. If the value of any of the operands is true then condition becomes true. | (X or Y) is true. |