Operators in PHP Explained

    Atul Kabra3 min readUpdated

    Operators in PHP

    Operators are symbols that perform actions on values, such as adding numbers, comparing two values, or combining conditions. PHP groups them into a few clear categories, and once you know these, most expressions in PHP become easy to read.

    Arithmetic operators

    These perform basic maths.

    <?php
    $a = 10;
    $b = 3;
    
    echo $a + $b;   // 13  addition
    echo $a - $b;   // 7   subtraction
    echo $a * $b;   // 30  multiplication
    echo $a / $b;   // 3.333... division
    echo $a % $b;   // 1   modulus (remainder)
    echo $a ** $b;  // 1000 exponent (10 to the power 3)
    ?>
    

    Assignment operators

    The = sign assigns a value. Combined operators are shortcuts.

    <?php
    $total = 100;
    
    $total += 20;  // same as $total = $total + 20  -> 120
    $total -= 10;  // -> 110
    $total *= 2;   // -> 220
    
    echo $total;
    ?>
    

    Comparison operators

    These compare two values and return a boolean (true or false). The most important distinction for beginners is == versus ===.

    <?php
    var_dump(5 == "5");   // true  -> loose: compares value only
    var_dump(5 === "5");  // false -> strict: value AND type must match
    
    var_dump(10 != 8);    // true  -> not equal
    var_dump(10 > 8);     // true
    var_dump(10 <= 10);   // true
    ?>
    
    • == checks if values are equal after type juggling.
    • === checks if values and types are equal. Prefer === to avoid surprises.

    The spaceship operator

    The <=> operator returns -1, 0, or 1 depending on whether the left value is less than, equal to, or greater than the right. It is handy for sorting.

    <?php
    echo 1 <=> 2;  // -1 (left is smaller)
    echo 2 <=> 2;  //  0 (equal)
    echo 3 <=> 2;  //  1 (left is greater)
    ?>
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Logical operators

    These combine boolean conditions.

    <?php
    $loggedIn = true;
    $isAdmin  = false;
    
    // AND: both must be true
    var_dump($loggedIn && $isAdmin);  // false
    
    // OR: at least one must be true
    var_dump($loggedIn || $isAdmin);  // true
    
    // NOT: reverses a boolean
    var_dump(!$isAdmin);              // true
    ?>
    

    String operators

    PHP uses the dot (.) to join strings together (concatenation).

    <?php
    $first = "Web";
    $second = "Development";
    
    echo $first . " " . $second; // Web Development
    
    $first .= " Design";          // append to the existing string
    echo $first;                  // Web Design
    ?>
    

    The null coalescing operator

    ?? returns the left value if it exists and is not null, otherwise the right value. It is great for default values.

    <?php
    // Imagine $username may or may not be set.
    $displayName = $username ?? "Guest";
    echo $displayName; // "Guest" if $username is not set
    ?>
    

    Operator precedence

    Like in maths, some operators run before others. Multiplication happens before addition, for example. When in doubt, use parentheses to make the order explicit and readable.

    <?php
    echo 2 + 3 * 4;     // 14 (multiplication first)
    echo (2 + 3) * 4;   // 20 (parentheses first)
    ?>
    

    Common mistakes

    • Using == when you mean ===. Loose comparison can match values you did not intend, such as 0 == "abc" behaviour across versions.
    • Confusing = (assignment) with == (comparison). if ($x = 5) assigns rather than compares.
    • Forgetting the dot for string joining. PHP uses ., not +, to concatenate strings.
    • Relying on precedence instead of parentheses. Parentheses make intent obvious.

    FAQ

    When should I use === over ==? Almost always prefer ===. It avoids type-juggling surprises by requiring both value and type to match.

    What does % do? It returns the remainder of a division. For example, 10 % 3 is 1.

    What is ?? used for? It supplies a default value when a variable is null or not set, keeping code short and safe.

    Keep learning

    Back to the PHP & MySQL hub, revisit Variables & Data Types in PHP, or continue to Control Flow in PHP.

    Build real fluency with guided practice. Join the waitlist for the PHP course at Infoplanet in Jalgaon.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides