CodeNewbie Community 🌱

Montasser
Montasser

Posted on

PHP Arrow Functions: How They Work with Examples

PHP arrow functions offer a shorter syntax for writing simple anonymous functions. They use the fn keyword and return a single expression. Introduced in PHP 7.4, they reduce code repetition, improve readability, and make functional operations simpler.

This guide explains their syntax, behavior, scope, use cases, and differences from traditional anonymous functions.

Syntax of Arrow Function

Arrow functions use a compact one-liner syntax which is fn.

fn(parameter_list) => expression;
Enter fullscreen mode Exit fullscreen mode

Here is an example:

$square = fn($n) => $n * $n;
echo $square(4); // 16
Enter fullscreen mode Exit fullscreen mode
  • fn starts the arrow function.
  • (parameter_list) defines the inputs.
  • => separates the input from the output.
  • The expression must return a value.

The arrow function always returns the result of the expression. It cannot return null unless the expression itself returns null.

Arrow functions cannot include statements or blocks.

Here is a valid syntax:

fn($x) => $x + 1;
Enter fullscreen mode Exit fullscreen mode

But this is invalid:

fn($x) => { return $x + 1; }; // Error: use of block not allowed
Enter fullscreen mode Exit fullscreen mode

There is no return keyword. The arrow function automatically returns the evaluated result of the expression.

Scope and Binding

Arrow functions inherit variables from the parent scope automatically. This is the most important difference from traditional anonymous functions.

Here is an example:

$tax = 0.2;
$priceWithTax = fn($price) => $price * (1 + $tax);
Enter fullscreen mode Exit fullscreen mode

No need to use use ($tax). The function captures $tax automatically. This is called lexical scoping.

Anonymous functions require an explicit use clause:

$priceWithTax = function($price) use ($tax) {
    return $price * (1 + $tax);
};
Enter fullscreen mode Exit fullscreen mode

Arrow functions remove this boilerplate.

Capturing $this

Arrow functions also inherit the value of $this. Traditional closures do not.

For example:

class Cart {
    public $discount = 0.1;

    public function getPriceCalculator() {
        return fn($price) => $price * (1 - $this->discount);
    }
}
Enter fullscreen mode Exit fullscreen mode

The arrow function can access $this->discount without extra code.

In contrast, a traditional anonymous function would require careful handling or use of use().

Arrow functions can take any number of parameters. You can use:

  • Typed parameters
  • Default values
  • Variadic arguments

Here is another example:

$sum = fn(int ...$nums) => array_sum($nums);
echo $sum(1, 2, 3); // 6
Enter fullscreen mode Exit fullscreen mode

They do not support references or unpacking in return.

PHP arrow functions can specify a return type.

$half = fn(float $x): float => $x / 2;
Enter fullscreen mode Exit fullscreen mode

The return type ensures strict typing, same as normal functions.

Summary

Arrow functions simplify short closures. They remove boilerplate. They support variable capture and $this binding automatically.

Use them for:

  • Callbacks
  • Array transformations
  • Functional logic
  • Scoped value returns

Thank you for reading. Follow this blog for more PHP tutorials:
https://flatcoding.com

Top comments (1)

Collapse
 
keithwalker profile image
Keithwalker

PHP arrow functions are a shorter way to write anonymous functions introduced in PHP 7.4. They use the fn keyword and always return a single expression without needing the return statement. Unlike traditional closures, arrow functions automatically inherit variables from the parent scope and $this, making the code cleaner. They’re great for callbacks, array operations, and simple calculations. If you need expert PHP solutions or want to build efficient apps, you can hire PHP developers to get it done faster.