> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP Match Expression

The `match` expression is a control structure introduced in PHP 8.0 that evaluates a subject value against multiple conditions and returns the value of the first matching expression. Unlike the traditional `switch` statement, `match` is an expression that evaluates to a value, enforces strict type comparison, and does not exhibit implicit fallthrough behavior.

```php theme={"dark"}
<?php
$statusCode = 200;

$message = match ($statusCode) {
    200, 201 => 'Request successful',
    400 => 'Bad request',
    404 => 'Not found',
    default => 'Unknown status',
};

echo $message; // Outputs: Request successful
```

## Core Mechanics

* **Expression Evaluation:** Because `match` is an expression, it evaluates to a single value. This result can be directly assigned to a variable, returned from a function, or passed as an argument.
* **Strict Comparison:** The `match` expression compares the subject to the conditions using strict identity (`===`). Both the value and the data type must match exactly. A string `"1"` will not match an integer `1`.
* **No Fallthrough:** Execution strictly terminates after the first matching arm is evaluated. There is no implicit fallthrough to subsequent conditions, eliminating the need for `break` statements.
* **Comma-Separated Conditions:** A single match arm can evaluate multiple conditions separated by commas. If any condition in the list strictly matches the subject, the corresponding right-hand expression is evaluated and returned.
* **Exhaustiveness:** The `match` expression must be exhaustive. If the subject does not match any condition and no `default` arm is provided, PHP will throw an `UnhandledMatchError` at runtime.
* **Single Expression Arms:** The right side of a match arm (`=>`) must be a single expression. It cannot be a multi-line block of statements.

## Execution Behavior

When the PHP engine encounters a `match` expression, it executes using the following sequence:

1. The subject expression is evaluated once.
2. The engine iterates through the match arms sequentially from top to bottom.
3. For each arm, the condition(s) on the left side of the `=>` operator are evaluated.
4. If a strict match (`===`) is found, the single expression on the right side of the `=>` is evaluated, and its result is returned as the value of the entire `match` block.
5. If no match is found in the specific conditions, the `default` arm is evaluated and returned.
6. If no match is found and no `default` arm exists, an exception is thrown.

## Strict Type Comparison Visualization

```php theme={"dark"}
<?php
$subject = '2';

$result = match ($subject) {
    1, 2 => 'Integer match',       // Fails: Strict comparison ('2' !== 2)
    '1' => 'String one',           // Fails: '2' !== '1'
    '2' => 'String two',           // Succeeds: '2' === '2'
    default => 'Fallback value',   // Ignored due to prior match
};

echo $result; // Outputs: String two
```

## Advanced Evaluation (True Subject)

The `match` expression can also be used with `true` as the subject to evaluate arbitrary conditional expressions, functioning similarly to a chained `if-elseif` structure but returning a value. In this pattern, the engine evaluates each condition until one resolves to boolean `true`.

```php theme={"dark"}
<?php
$value = 75;

$result = match (true) {
    $value > 100 => 'High',
    $value > 50 => 'Medium',
    $value > 0 => 'Low',
    default => 'Zero or Negative',
};

echo $result; // Outputs: Medium
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
