Skip to content

Operators

Operators in PHP are special symbols or keywords that are used to perform operations on variables and values. PHP supports a wide range of operators, including arithmetic, assignment, comparison, logical, and more. Here’s an overview of some common operators in PHP:

1. Arithmetic Operators:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus – remainder of division)
  • ** (Exponentiation)

2. Assignment Operators:

  • = (Assignment)
  • += (Add and assign)
  • -= (Subtract and assign)
  • *= (Multiply and assign)
  • /= (Divide and assign)
  • %= (Modulus and assign)

3. Comparison Operators:

  • == (Equal)
  • === (Identical – both value and type)
  • != or <> (Not equal)
  • !== (Not identical)
  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)

4. Logical Operators:

  • && or and (Logical AND)
  • || or or (Logical OR)
  • ! or not (Logical NOT)
  • xor (Exclusive OR)

5. Increment/Decrement Operators:

  • ++ (Increment by 1)
  • -- (Decrement by 1)

6. Concatenation Operator:

  • . (Concatenation – used to join strings)

7. Ternary Operator:

  • ? : (Conditional operator, used for simple if-else conditions)

8. Null Coalescing Operator:

  • ?? (Returns the first non-null value from a list of expressions)

9. Type Operators:

  • instanceof (Checks if an object is an instance of a specific class)

10. Bitwise Operators:

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • << (Left shift)
  • >> (Right shift)

11. Error Control Operator:

  • @ (Used to suppress errors)

12. Array Operators:

  • + (Union – Combines two arrays)
  • == (Equality – Checks if two arrays have the same key/value pairs)
  • === (Identity – Checks if two arrays are identical)

These operators allow you to perform various operations, make decisions, and manipulate data in PHP code. Depending on your needs, you can use these operators to create complex expressions and perform a wide range of tasks in your PHP programs.