Operators in PHP

 

Operators in PHP are special symbols used to perform calculations and manipulate data. They are categorized based on their function:
 
1. Arithmetic Operators
Used for standard mathematical operations:
  • + (Addition), - (Subtraction), * (Multiplication), / (Division)
  • % (Modulus): Returns the remainder of a division (e.g., 10 % 3 is 1).
  • ** (Exponentiation): x to the power of y  (e.g., 2 ** 3 is 8).
 
2. Assignment Operators
Used to write values to variables:
  • = : Standard assignment ($x = 10).
  • +=, -=, *=, /= : Combined operators. For example, $x += 5 is the same as $x = $x + 5.
 
3. Comparison Operators
Used to compare two values, returning true or false:
  • == (Equal): Checks if values are the same.
  • === (Identical): Checks if values and data types are the same.
  • != or <> (Not equal).
  • >, <, >=, <= (Greater than, Less than, etc.).
 
4. Increment/Decrement Operators
Used to increase or decrease a variable’s value by one:
  • ++$x (Pre-increment) or $x++ (Post-increment).
  • --$x (Pre-decrement) or $x-- (Post-decrement).
 
5. Logical Operators
Used to combine conditional statements:
  • and / && : True if both statements are true.
  • or / || : True if at least one statement is true.
  • ! (Not): Reverses the result (true becomes false).
 
6. String Operators
Specifically for manipulating text:
  • . (Concatenation): Joins two strings together (e.g., "Hello" . " World").
  • .= (Concatenation assignment): Appends a string to a variable.