Php operators and examples

Php operators and examples

In this article lets understand php operators and its examples.

Operators in PHP are symbols that works with variable to perform various operations. PHP supports many operators and we an categorize them from how it functions as below:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Increment and Decrement Operators
  • Concatenation Operator
  • Ternary Operator
  • Null Coalescing Operator
  • Type Operators

1. Arithmetic Operators

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (remainder after division)
$a = 10;
$b = 5;

// Addition
$result1 = $a + $b; // $result1 will be 15

// Subtraction
$result2 = $a - $b; // $result2 will be 5

// Multiplication
$result3 = $a * $b; // $result3 will be 50

// Division
$result4 = $a / $b; // $result4 will be 2

// Modulus (Remainder)
$result5 = $a % $b; // $result5 will be 0

2. Assignment Operators

  • = Assignment
  • += Addition assignment
  • -= Subtraction assignment
  • *= Multiplication assignment
  • /= Division assignment
  • %= Modulus assignment
$x = 10;

// Addition assignment
$x += 5; // $x is now 15

// Subtraction assignment
$x -= 3; // $x is now 12

// Multiplication assignment
$x *= 2; // $x is now 24

// Division assignment
$x /= 3; // $x is now 8

// Modulus assignment
$x %= 5; // $x is now 3

3. Comparison Operators

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

Comparison operators in PHP are used to compare two values and return a Boolean result (true or false). They are commonly used in conditional statements, loops, and various other situations where you need to make decisions based on the comparison results.

$a = 10;
$b = 5;

// Equal to
if ($a == $b) {
    echo "a is equal to b";
}

// Not equal to
if ($a != $b) {
    echo "a is not equal to b\n"; //Output: "a is not equal to b"
}

// Greater than
if ($a > $b) {
    echo "a is greater than b"; // Output: "a is greater than b"
}

// Less than
if ($a < $b) {
    echo "a is less than b\n";
}

// Greater than or equal to
if ($a >= $b) {
    echo "a is greater than or equal to b\n";
}

// Less than or equal to
if ($a <= $b) {
    echo "a is less than or equal to b"; // Output: "a is less than or equal to b"
}

--------------------------------------------------------------------------------
$num1 = 5;
$num2 = "5";

if ($num1 === $num2) {
    echo "num1 and num2 are identical.";
} else {
    echo "num1 and num2 are not identical.";
}
//Output : num1 and num2 are not identical.

if ($num1 !== $num2) {
    echo "num1 and num2 are not identical.";
} else {
    echo "num1 and num2 are identical.";
}
// Output : num1 and num2 are not identical.

4. Logical Operators

  • && Logical AND
  • || Logical OR
  • ! Logical NOT
$isTrue = true;
$isFalse = false;

// Logical AND
if ($isTrue && $isFalse) {
    echo "Both conditions are true";
} else {
    echo "At least one condition is false"; // Output: "At least one condition is false"
}

// Logical OR
if ($isTrue || $isFalse) {
    echo "At least one condition is true"; // Output: "At least one condition is true"
}

// Logical NOT
if (!$isFalse) {
    echo "The condition is not false"; // Output: "The condition is not false"
}

5. Increment and Decrement Operators

  • ++ Increment
  • -- Decrement
$counter = 5;
echo "Original value: " . $counter . "<br>"; // Output: Original value: 5

// Pre-increment
++$counter;
echo "After pre-increment: " . $counter . "<br>"; // Output: After pre-increment: 6

------------------------------------------------------------------------

$counter = 5;
echo "Original value: " . $counter . "<br>"; // Output: Original value: 5

// Post-increment
$counter++;
echo "After post-increment: " . $counter . "<br>"; // Output: After post-increment: 6

------------------------------------------------------------------------

$counter = 5;
echo "Original value: " . $counter . "<br>"; // Output: Original value: 5

// Pre-decrement
--$counter;
echo "After pre-decrement: " . $counter . "<br>"; // Output: After pre-decrement: 4

-----------------------------------------------------------------------

$counter = 5;
echo "Original value: " . $counter . "<br>"; // Output: Original value: 5

// Post-decrement
$counter--;
echo "After post-decrement: " . $counter . "<br>"; // Output: After post-decrement: 4

6. Concatenation Operator

  • . Concatenation (used to join strings)
$string1 = "coding";
$string2 = "solutions";
$string3 = "point";

$fullString = $coding. " " . $solutions." ".point; 
//Output : "coding solutions point"

7. Ternary Operator

  • expr1 ? expr2 : expr3 => If expr1 is true, then expr2 is evaluated; otherwise, expr3 is evaluated.
$a = 10;
$b = 5;

$maxValue = ($a > $b) ? $a : $b; 
//Output : 10

8. Null Coalescing Operator

  • ?? Returns the first operand if it exists and is not null; otherwise, returns the second operand.
$name = null;
$displayName = $name ?? "Guest";

echo $displayName; // Output: "Guest"

9. Type Operators

  • instanceof Checks if an object is an instance of a specified class or implements a specified interface.
class Dog extends Animal {
    public function makeSound() {
        echo "Woof!\n";
    }
}

// Create instances of different classes
$dog = new Dog();

// Check the type using instanceof
if ($dog instanceof Dog) {
    echo "The dog is an instance of Dog.\n";
}

That’s it on Php operators and examples.

See other articles on PHP here.