Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 9

Operators & the match Expression

PHP operators cover arithmetic, comparison, logical, assignment, and string operations. PHP 8.0 added the match expression - a safer, more expressive replacement for switch that uses strict comparison and throws an error for unhandled values.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

All Key Operators

All Key Operators
<?php
declare(strict_types=1);

// ── Arithmetic ──────────────────────────────────────────────────
$a = 10; $b = 3;
echo $a + $b;    // 13
echo $a - $b;    // 7
echo $a * $b;    // 30
echo $a / $b;    // 3.333...
echo $a % $b;    // 1 (modulo)
echo $a ** $b;   // 1000 (exponentiation)
echo intdiv($a, $b); // 3 (integer division)

// ── Comparison ─────────────────────────────────────────────────
var_dump(5 == "5");   // true (loose - type coercion)
var_dump(5 === "5");  // false (strict - type + value)
var_dump(5 <=> 3);    // 1  (spaceship - positive if left > right)
var_dump(3 <=> 5);    // -1 (negative if left < right)
var_dump(3 <=> 3);    // 0  (equal)

// ── Logical ─────────────────────────────────────────────────────
$loggedIn = true;
$isAdmin  = false;
var_dump($loggedIn && $isAdmin);  // false (AND)
var_dump($loggedIn || $isAdmin);  // true  (OR)
var_dump(!$loggedIn);             // false (NOT)

// ── Assignment shorthand ─────────────────────────────────────────
$count = 0;
$count += 5;    // $count = $count + 5
$count -= 2;    // 3
$count *= 3;    // 9
$str = "Hello";
$str .= " World";  // string concatenation assignment → "Hello World"

// ── Null Coalescing ──────────────────────────────────────────────
$name = $_POST["name"] ?? "Anonymous";    // default if not set/null
$name ??= "Default";  // assign only if $name is null (PHP 7.4+)

// ── Ternary ──────────────────────────────────────────────────────
$status = $loggedIn ? "Active" : "Guest";   // full ternary
$display = $name ?: "Unknown";              // Elvis - uses $name if truthy

// ── Type casting ─────────────────────────────────────────────────
$strNum = "42abc";
$int    = (int) $strNum;     // 42
$float  = (float) "3.14";   // 3.14
$bool   = (bool) "";         // false (empty string is falsy)
$arr    = (array) "hello";  // ["hello"]

match Expression (PHP 8.0)

match Expression (PHP 8.0)
<?php
declare(strict_types=1);

$statusCode = 404;

// switch - loose comparison, fall-through risk
switch ($statusCode) {
    case 200:
    case 201:
        $message = "Success";
        break;
    case 404:
        $message = "Not Found";
        break;
    default:
        $message = "Unknown";
}

// match - strict comparison (===), no fall-through, must be exhaustive, returns value
$message = match($statusCode) {
    200, 201 => "Success",
    301, 302 => "Redirect",
    400      => "Bad Request",
    401      => "Unauthorized",
    403      => "Forbidden",
    404      => "Not Found",
    500      => "Server Error",
    default  => "Unknown Status",
};
// If no arm matches and no default → UnhandledMatchError thrown

// match with no-match condition
$role = "admin";
$permissions = match(true) {
    $role === "admin"     => ["read", "write", "delete"],
    $role === "editor"    => ["read", "write"],
    $role === "viewer"    => ["read"],
    default               => [],
};

Quick Quiz

Tip

Tip

Practice Operators the match Expression in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Operators & the match Expression

Practice Task

Note

Practice Task - (1) Write a working example of Operators the match Expression from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Operators the match Expression is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done