> ## 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 Arrow Function

Arrow functions, introduced in PHP 7.4, are a concise syntax for anonymous functions (closures) that implicitly return the value of a single evaluated expression and automatically capture variables from the parent scope by value.

## Syntax

The fundamental signature of an arrow function utilizes the `fn` keyword followed by parameters, the double-arrow operator (`=>`), and a single expression. It can optionally be prefixed with the `static` keyword.

```php theme={"dark"}
[static] fn([Type $param, ...]): ReturnType => expression;
```

## Core Mechanics

**1. Implicit Return**
Arrow functions do not use the `return` keyword. The expression on the right side of the `=>` operator is evaluated, and its result is automatically returned.

**2. Automatic Scope Binding (By-Value)**
Unlike standard closures in PHP, which require explicit variable binding using the `use` language construct, arrow functions automatically capture variables from the parent scope. This capture is strictly **by-value**.

```php theme={"dark"}
$multiplier = 5;

// Standard Closure
$standard = function($value) use ($multiplier) {
    return $value * $multiplier;
};

// Arrow Function equivalent
$arrow = fn($value) => $value * $multiplier;
```

Because the binding is by-value, modifying an auto-captured variable inside the arrow function will not mutate the variable in the outer scope.

```php theme={"dark"}
$counter = 0;

$increment = fn() => $counter++;
$increment();

// $counter remains 0 in the parent scope
```

**3. Single Expression Limitation**
Arrow functions are strictly limited to a single expression. They cannot contain multiple statements, block scopes defined by curly braces `{}`, or control structures like `if`/`else` or `switch` (though the ternary operator `?:` and `match` expressions are permitted as they evaluate to a single expression).

## Advanced Characteristics

* **`$this` Binding and `static` Declaration:** When an arrow function is declared within a class method, the `$this` context is automatically bound and accessible within the arrow function's expression, mirroring the behavior of standard closures. To prevent this automatic binding of `$this` and the current class scope, the arrow function can be declared as `static`. This is critical for memory management and avoiding unintended state mutations.

```php theme={"dark"}
class Example {
    public function getClosure(): Closure {
        // $this is bound
        return fn() => $this; 
    }

    public function getStaticClosure(): Closure {
        // $this is NOT bound; prevents keeping the object in memory
        return static fn($x) => $x * 2; 
    }
}
```

* **By-Reference Passing and Returning:** While scope capture is always by-value, you can define arrow functions that accept arguments by reference or return values by reference using the `&` operator. Because auto-captured variables are local copies, returning an auto-captured variable by reference returns a reference to the local copy, not the outer scope. A valid reference return must use an argument passed by reference or an object property.

```php theme={"dark"}
// Passing argument by reference
$modifyInPlace = fn(&$x) => $x *= 2;

// Returning by reference (using an argument passed by reference)
$returnRef = fn&(&$x) => $x;
```

* **Type Hinting:** Arrow functions fully support PHP's type system, including parameter types, return types, union types, and intersection types.

```php theme={"dark"}
$strictArrow = fn(int|float $a, int|float $b): int|float => $a + $b;
```

<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>
