Course Content
About Lesson

Operators in PHP

Operators are special symbols or keywords that are used to perform operations on variables and values.
In PHP, operators are grouped into categories: Arithmetic, Assignment, Comparison, and Logical.

These operators are used to perform mathematical calculations.

OperatorDescriptionExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (remainder)10 % 31
**Exponentiation (power)2 ** 38
PHP
<!DOCTYPE html>
<html>
<head>
    <title>Arithmetic Operators</title>
</head>
<body>
    <?php
    $a = 10;
    $b = 3;

    echo "<h2>Arithmetic Operators</h2>";
    echo "a + b = " . ($a + $b) . "<br>";
    echo "a - b = " . ($a - $b) . "<br>";
    echo "a * b = " . ($a * $b) . "<br>";
    echo "a / b = " . ($a / $b) . "<br>";
    echo "a % b = " . ($a % $b) . "<br>";
    echo "a ** b = " . ($a ** $b) . "<br>";
    ?>
</body>
</html>
  • $a + $b adds two numbers.
  • $a % $b gives remainder when $a is divided by $b.
  • $a ** $b means $a raised to the power $b.

Assignment operators are used to assign values to variables.

OperatorExampleSame as
=$x = 5Assigns 5 to $x
+=$x += 3$x = $x + 3
-=$x -= 3$x = $x - 3
*=$x *= 3$x = $x * 3
/=$x /= 3$x = $x / 3
%=$x %= 3$x = $x % 3
PHP
<?php
$x = 10;

echo "<h2>Assignment Operators</h2>";
echo "Initial value: $x <br>";

$x += 5;
echo "After x += 5: $x <br>";

$x -= 3;
echo "After x -= 3: $x <br>";

$x *= 2;
echo "After x *= 2: $x <br>";

$x /= 4;
echo "After x /= 4: $x <br>";

$x %= 3;
echo "After x %= 3: $x <br>";
?>
  • $x += 5 increases $x by 5.
  • $x *= 2 multiplies $x by 2.
  • $x %= 3 gives remainder after dividing $x by 3.

These are used to compare two values. They return true or false.

OperatorDescriptionExampleResult
==Equal to5 == "5"true
===Identical (equal + same data type)5 === "5"false
!= or <>Not equal5 != 3true
!==Not identical5 !== "5"true
>Greater than10 > 5true
<Less than10 < 5false
>=Greater than or equal10 >= 10true
<=Less than or equal10 <= 5false
<=>Spaceship (returns -1, 0, 1)10 <=> 20-1
PHP
<?php
$a = 10;
$b = "10";

echo "<h2>Comparison Operators</h2>";
var_dump($a == $b);   // true
echo "<br>";
var_dump($a === $b);  // false
echo "<br>";
var_dump($a != $b);   // false
echo "<br>";
var_dump($a !== $b);  // true
echo "<br>";
var_dump($a > 5);     // true
echo "<br>";
var_dump($a < 5);     // false
echo "<br>";
var_dump($a <=> 20);  // -1
?>
  • == checks only value.
  • === checks value + data type.
  • <=> is a spaceship operator: returns -1 if left < right, 0 if equal, 1 if left > right.

These are used to combine multiple conditions.

OperatorDescriptionExampleResult
&& or andTrue if both are true($x > 5 && $x < 15)true
`oror`True if any is true
!Not (negation)!($x > 5)false
xorTrue if one is true but not both(true xor false)true
    PHP
    <?php
    $x = 10;
    
    echo "<h2>Logical Operators</h2>";
    
    if ($x > 5 && $x < 15) {
        echo "x is between 5 and 15 <br>";
    }
    
    if ($x > 5 || $x < 5) {
        echo "x is greater than 5 OR less than 5 <br>";
    }
    
    if (!($x < 5)) {
        echo "x is NOT less than 5 <br>";
    }
    
    if (true xor false) {
        echo "One condition is true, but not both <br>";
    }
    ?>
    • && checks if both conditions are true.
    • || checks if at least one condition is true.
    • ! reverses the condition.
    • xor is true only when exactly one condition is true.