PHP operators


Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

  • Addition (+)

    • Adds two operands.
    • Example:
      <?php $sum = 5 + 3; // $sum = 8 ?>
  • Subtraction (-)

    • Subtracts the right operand from the left operand.
    • Example:
      <?php $difference = 5 - 3; // $difference = 2 ?>
  • Multiplication (*)

    • Multiplies two operands.
    • Example:
      <?php $product = 5 * 3; // $product = 15 ?>
  • Division (/)

    • Divides the left operand by the right operand.
    • Example:
      <?php $quotient = 10 / 2; // $quotient = 5 ?>
  • Modulus (%)

    • Returns the remainder of the division of the left operand by the right operand.
    • Example:
      <?php $remainder = 10 % 3; // $remainder = 1 ?>
  • Exponentiation (**)

    • Raises the left operand to the power of the right operand.
    • Example:
      <?php $power = 2 ** 3; // $power = 8 ?>

2. Assignment Operators

Assignment operators are used to assign values to variables. The most basic assignment operator is =.

  • Assignment (=)

    • Assigns the value of the right operand to the left operand.
    • Example:
      <?php $x = 10; // $x = 10 ?>
  • Combined Assignment Operators

    • Addition Assignment (+=)
      <?php $x = 10; $x += 5; // $x = 15 ?>
    • Subtraction Assignment (-=)
      <?php $x = 10; $x -= 3; // $x = 7 ?>
    • Multiplication Assignment (*=)
      <?php $x = 10; $x *= 2; // $x = 20 ?>
    • Division Assignment (/=)
      <?php $x = 10; $x /= 2; // $x = 5 ?>
    • Modulus Assignment (%=)
      <?php $x = 10; $x %= 3; // $x = 1 ?>
    • Exponentiation Assignment (**=)
      <?php $x = 2; $x **= 3; // $x = 8 ?>

3. Comparison Operators

Comparison operators are used to compare two values and return a boolean value (true or false).

  • Equal (==)

    • Checks if two values are equal.
    • Example:
      <?php $result = (5 == 5); // $result = true ?>
  • Identical (===)

    • Checks if two values are equal and of the same type.
    • Example:
      <?php $result = (5 === "5"); // $result = false ?>
  • Not Equal (!= or < >)

    • Checks if two values are not equal.
    • Example:
      <?php $result = (5 != 3); // $result = true ?>
  • Not Identical (!==)

    • Checks if two values are not equal or not of the same type.
    • Example:
      <?php $result = (5 !== "5"); // $result = true ?>
  • Greater Than (>)

    • Checks if the left operand is greater than the right operand.
    • Example:
      <?php $result = (10 > 5); // $result = true ?>
  • Less Than (<)

    • Checks if the left operand is less than the right operand.
    • Example:
      <?php $result = (3 < 5); // $result = true ?>
  • Greater Than or Equal To (>=)

    • Checks if the left operand is greater than or equal to the right operand.
    • Example:
      <?php $result = (5 >= 5); // $result = true ?>
  • Less Than or Equal To (<=)

    • Checks if the left operand is less than or equal to the right operand.
    • Example:
      <?php $result = (3 <= 5); // $result = true ?>
  • Spaceship (<=>)

    • Compares two values and returns -1, 0, or 1 when the left operand is less than, equal to, or greater than the right operand, respectively.
    • Example:
      <?php $result = 5 <=> 3; // $result = 1 $result = 5 <=> 5; // $result = 0 $result = 3 <=> 5; // $result = -1 ?>

4. Increment/Decrement Operators

These operators are used to increment or decrement a variable’s value by one.

  • Increment (++)

    • Pre-increment (++$x): Increments $x by 1, then returns $x.
    • Post-increment ($x++): Returns $x, then increments $x by 1.
    • Example:
      <?php $x = 5; echo ++$x; // Outputs: 6 echo $x++; // Outputs: 6, then $x becomes 7 ?>
  • Decrement (--)

    • Pre-decrement (--$x): Decrements $x by 1, then returns $x.
    • Post-decrement ($x--): Returns $x, then decrements $x by 1.
    • Example:
      <?php $x = 5; echo --$x; // Outputs: 4 echo $x--; // Outputs: 4, then $x becomes 3 ?>

5. Logical Operators

Logical operators are used to combine conditional statements.

  • And (&& or and)

    • Returns true if both operands are true.
    • Example:
      <?php $result = (true && false); // $result = false ?>
  • Or (|| or or)

    • Returns true if either operand is true.
    • Example:
      <?php $result = (true || false); // $result = true ?>
  • Not (!)

    • Returns true if the operand is false.
    • Example:
      <?php $result = !true; // $result = false ?>
  • Xor (xor)

    • Returns true if one, and only one, of the operands is true.
    • Example:
      <?php $result = (true xor false); // $result = true ?>

6. String Operators

String operators are used to manipulate and concatenate strings.

  • Concatenation (.)

    • Joins two strings together.
    • Example:
      <?php $greeting = "Hello, " . "World!"; // $greeting = "Hello, World!" ?>
  • Concatenation Assignment (.=)

    • Appends the right operand to the left operand.
    • Example:
      <?php $greeting = "Hello"; $greeting .= ", World!"; // $greeting = "Hello, World!" ?>

7. Array Operators

Array operators are used to compare or merge arrays.

  • Union (+)

    • Combines two arrays. If a key exists in both arrays, the value from the left array will be used.
    • Example:
      <?php $array1 = array("a" => "apple", "b" => "banana"); $array2 = array("b" => "berry", "c" => "cherry"); $result = $array1 + $array2; // $result = array("a" => "apple", "b" => "banana", "c" => "cherry") ?>
  • Equality (==)

    • Returns true if two arrays have the same key-value pairs.
    • Example:
      <?php $array1 = array("a" => "apple", "b" => "banana"); $array2 = array("a" => "apple", "b" => "banana"); $result = ($array1 == $array2); // $result = true ?>
  • Identity (===)

    • Returns true if two arrays have the same key-value pairs in the same order and of the same types.
    • Example:
      <?php $array1 = array("a" => "apple", "b" => "banana"); $array2 = array("a" => "apple", "b" => "banana"); $result = ($array1 === $array2); // $result = true ?>
  • Inequality (!= or < >)

    • Returns true if two arrays do not have the same key-value pairs.
    • Example:
      <?php $array1 = array("a" => "apple", "b" => "banana"); $array2 = array("a" => "apple", "b" => "berry"); $result = ($array1 != $array2); // $result = true ?>
  • Non-Identity (!==)

    • Returns true if two arrays do not have the same key-value pairs or are of different types.
    • Example:
      <?php $array1 = array("a" => "apple", "b" => "banana"); $array2 = array("a" => "apple", "b" => "berry"); $result = ($array1 !== $array2); // $result = true ?>

8. Ternary Operator

The ternary operator is a shorthand way of writing a simple if-else statement.

  • Syntax:

    condition ? true_value : false_value
  • Example:

    <?php $age = 18; $status = ($age >= 18) ? "adult" : "minor"; // $status = "adult" ?>

9. Null Coalescing Operator (??)

The null coalescing operator is used to return the first non-null value among its operands.

  • Example:
    <?php $username = $_GET['user'] ?? 'guest'; // If 'user' is not set, 'guest' is used ?>