Skip to Content
Course content

6: Operators and Expressions

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, why are there two different "equals" signs?

This is one of the first things that trips people up in PHP. You'll see == (loose equality) and === (strict equality). The difference is that == tries to be "helpful" by converting types before comparing them. We call this type juggling.

$age = "25"; // This is a string

if ($age == 25) {
    echo "This is true!"; // PHP converts the string "25" to an integer 25
}

if ($age === 25) {
    echo "This will never run."; // The types don't match (string vs integer)
}

In my experience, relying on == is a recipe for weird bugs down the road. I almost always use ===. It forces you to be explicit about your data types, which makes your code much easier to debug when things go sideways.

How do I handle complex logic without the code becoming a mess?

When you're building real-world features, you're rarely checking just one thing. You'll use logical operators like && (AND) and || (OR) to combine expressions. The trick to keeping this readable is using parentheses to group your logic, just like you would in math.

Imagine you're checking if a user can edit a blog post. They need to be logged in, and they must either be an administrator OR the original author of the post:

$isLoggedIn = true;
$isAdmin = false;
$isAuthor = true;

if ($isLoggedIn && ($isAdmin || $isAuthor)) {
    echo "Access granted to the editor.";
} else {
    echo "You don't have permission to do that.";
}

Without those parentheses, the order of operations might not do what you expect. I usually spend a few extra seconds adding parentheses even when they aren't strictly required—it saves the next developer (which might be you in six months) from having to guess your intent.

What's the deal with these shorthand assignment operators?

You'll often see things like += or .=. These are just shortcuts. Instead of writing $x = $x + 5, you can just write $x += 5. It's cleaner and faster to type.

The .= operator is particularly useful in PHP for building long strings. I use this all the time when generating HTML blocks or building a query string:

$message = "Welcome to the site, ";
$name = "Alex";
$message .= $name; // Appends $name to the end of $message
$message .= "! We're glad you're here.";

echo $message; // Output: Welcome to the site, Alex! We're glad you're here.

Where would I actually use the modulo (%) operator?

The modulo operator doesn't give you the result of a division; it gives you the remainder. At first, it feels like a math class leftover, but it's incredibly useful in UI development.

A classic use case is alternating row colors in a table. If the remainder of the current row index divided by 2 is 0, the row is even; otherwise, it's odd:

for ($i = 1; $i <= 5; $i++) {
    if ($i % 2 === 0) {
        echo "Row $i: Grey background\n";
    } else {
        echo "Row $i: White background\n";
    }
}

I've also used it for "every Xth" logic, like triggering a "Save Progress" popup every 10 pages a user visits.




📋 Practical Task

Build a Loyalty Reward Validator

You are building a checkout system. Write a PHP script that determines if a customer qualifies for a "VIP Discount" based on the following rules:

  • The user must have a $totalSpent of 500 or more.
  • The user must either have a $membershipYears of 2 or more OR have a $isPremiumMember boolean set to true.
  • Use strict comparison (===) where applicable.

Create three variables to represent these values, then use a single if statement with logical operators to print "Discount Applied" or "Regular Price".

Rating
0 0

There are no comments for now.

to be the first to leave a comment.