PHP Operators

Performing operations on variables and values

🔧 What are PHP Operators?

Operators are symbols that perform operations on variables and values. PHP supports various operator types including arithmetic, comparison, logical, and assignment operators for manipulating data.


<?php
$a = 10;
$b = 5;
$sum = $a + $b; // Addition operator
echo $sum; // Outputs: 15
?>
                                    

Output:

15

Types of Operators

Arithmetic

Perform mathematical calculations like addition, subtraction, multiplication, division, and modulus operations on numeric values.

<?php
$x = 10 + 5; // 15
$y = 10 - 5; // 5
$z = 10 * 5; // 50
?>
📊

Comparison

Compare two values and return true or false. Used in conditional statements to make decisions.

<?php
$a = 10;
$b = 5;
var_dump($a > $b); // true
?>
🔗

Logical

Combine conditional statements using AND, OR, NOT operators to create complex logical expressions.

<?php
$x = true;
$y = false;
var_dump($x && $y); // false
?>
✏️

Assignment

Assign values to variables using equals sign and combined assignment operators for shorthand operations.

<?php
$x = 10;
$x += 5; // Same as $x = $x + 5
echo $x; // 15
?>

🔹 Arithmetic Operators

Perform basic mathematical operations:

<?php
$a = 10;
$b = 3;

echo "Addition: " . ($a + $b);        // 13
echo "<br>";
echo "Subtraction: " . ($a - $b);     // 7
echo "<br>";
echo "Multiplication: " . ($a * $b);  // 30
echo "<br>";
echo "Division: " . ($a / $b);        // 3.333...
echo "<br>";
echo "Modulus: " . ($a % $b);         // 1
echo "<br>";
echo "Exponentiation: " . ($a ** 2);  // 100
?>

Output:

Addition: 13

Subtraction: 7

Multiplication: 30

Division: 3.3333333333333

Modulus: 1

Exponentiation: 100

🔹 Comparison Operators

Compare values and return boolean results:

<?php
$x = 10;
$y = 5;

var_dump($x == $y);   // Equal: false
echo "<br>";
var_dump($x != $y);   // Not equal: true
echo "<br>";
var_dump($x > $y);    // Greater than: true
echo "<br>";
var_dump($x < $y);    // Less than: false
echo "<br>";
var_dump($x >= 10);   // Greater or equal: true
echo "<br>";
var_dump($x <= 5);    // Less or equal: false
echo "<br>";

// Identical (same value and type)
var_dump(10 === "10");  // false
echo "<br>";
var_dump(10 === 10);    // true
?>

Output:

bool(false)

bool(true)

bool(true)

bool(false)

bool(true)

bool(false)

bool(false)

bool(true)

🔹 Logical Operators

Combine multiple conditions:

<?php
$age = 25;
$hasLicense = true;

// AND operator (&&)
if ($age >= 18 && $hasLicense) {
    echo "Can drive!";
}
echo "<br>";

// OR operator (||)
$isWeekend = false;
$isHoliday = true;
if ($isWeekend || $isHoliday) {
    echo "No work today!";
}
echo "<br>";

// NOT operator (!)
$isRaining = false;
if (!$isRaining) {
    echo "Go outside!";
}
?>

Output:

Can drive!

No work today!

Go outside!

🔹 Assignment Operators

Assign and modify variable values:

<?php
$x = 10;
echo "Initial: $x";
echo "<br>";

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

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

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

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

$text = "Hello";
$text .= " World";  // String concatenation
echo $text;
?>

Output:

Initial: 10

After += 5: 15

After -= 3: 12

After *= 2: 24

After /= 4: 6

Hello World

🔹 Increment/Decrement Operators

Increase or decrease values by one:

<?php
$count = 5;

// Post-increment (use then increase)
echo "Post-increment: " . $count++;  // Shows 5
echo "<br>";
echo "After: $count";                // Now 6
echo "<br>";

// Pre-increment (increase then use)
echo "Pre-increment: " . ++$count;   // Shows 7
echo "<br>";

// Post-decrement
echo "Post-decrement: " . $count--;  // Shows 7
echo "<br>";
echo "After: $count";                // Now 6
echo "<br>";

// Pre-decrement
echo "Pre-decrement: " . --$count;   // Shows 5
?>

Output:

Post-increment: 5

After: 6

Pre-increment: 7

Post-decrement: 7

After: 6

Pre-decrement: 5

🔹 String Operators

Concatenate strings together:

<?php
$firstName = "John";
$lastName = "Doe";

// Concatenation operator (.)
$fullName = $firstName . " " . $lastName;
echo $fullName;
echo "<br>";

// Concatenation assignment (.=)
$message = "Hello";
$message .= " World";
$message .= "!";
echo $message;
echo "<br>";

// Combining with variables
$age = 25;
echo $firstName . " is " . $age . " years old.";
?>

Output:

John Doe

Hello World!

John is 25 years old.

🧠 Test Your Knowledge

What does the modulus operator (%) return?