Courses/PHP/Operators & Expressions

    Lesson 3 • Beginner

    Operators & Expressions ➕

    Master PHP's arithmetic, comparison, logical, and string operators — including the spaceship and null coalescing operators.

    What You'll Learn in This Lesson

    • • Arithmetic operators: +, -, *, /, %, ** (exponentiation)
    • • The critical difference between == (loose) and === (strict)
    • • PHP 7+ operators: spaceship <=> and null coalescing ??
    • • Logical operators: &&, ||, ! for combining conditions
    • • String concatenation with the . (dot) operator

    1️⃣ Operator Categories

    CategoryOperatorsExample
    Arithmetic+ - * / % **$a + $b
    Assignment= += -= *= /= .=$x += 5
    Comparison== === != !== < > <=>$a === $b
    Logical&& || ! and or$a && $b
    String. .="Hi " . $name
    Null Coalescing?? ??=$x ?? "default"

    Try It: Arithmetic & String Operators

    Practice arithmetic, assignment, increment/decrement, and string concatenation

    Try it Yourself »
    JavaScript
    // PHP Operators (simulated in JavaScript)
    console.log("=== Arithmetic Operators ===");
    console.log();
    
    let a = 15;
    let b = 4;
    
    console.log("$a = " + a + ", $b = " + b);
    console.log();
    console.log("$a + $b  = " + (a + b) + "   (Addition)");
    console.log("$a - $b  = " + (a - b) + "  (Subtraction)");
    console.log("$a * $b  = " + (a * b) + "  (Multiplication)");
    console.log("$a / $b  = " + (a / b) + " (Division — returns float!)");
    console.log("$a % $b  = " + (a % b) + "   (Modulus — remainder)");
    co
    ...

    Try It: Comparison & Logical Operators

    Learn the crucial difference between == and ===, plus PHP 7+ operators

    Try it Yourself »
    JavaScript
    // PHP Comparison & Logical Operators
    console.log("=== Comparison Operators ===");
    console.log();
    
    // The crucial difference: == vs ===
    console.log("IMPORTANT: == (loose) vs === (strict)");
    console.log();
    
    console.log("  0 == false    → " + (0 == false) + "    (loose: type juggling!)");
    console.log('  0 === false   → ' + (0 === false) + "   (strict: different types)");
    console.log();
    console.log('  "5" == 5      → ' + ("5" == 5) + "    (loose: string converted)");
    console.log('  "5" === 5     → 
    ...

    ⚠️ Common Mistakes

    ⚠️
    Using == instead of ===0 == "hello" is true in PHP! Always prefer ===.
    ⚠️
    Using + for strings — PHP uses . for concatenation. "5" + "3" equals 8 (numeric addition!).
    ⚠️
    Confusing = with ==if ($x = 5) assigns 5 to $x (always true). Use == or === for comparison.
    💡
    Pro Tip: Use ?? (null coalescing) instead of isset() checks — it's cleaner and more readable.

    📋 Quick Reference — PHP Operators

    OperatorMeaningExample
    ===Strict equal$a === $b
    <=>Spaceship (3-way)$a <=> $b → -1/0/1
    ??Null coalescing$x ?? "default"
    .String concat"Hi " . $name
    **Exponentiation2 ** 10 → 1024

    🎉 Lesson Complete!

    You've mastered PHP operators! Next, learn how to control program flow with if/else and switch statements.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service