๐Ÿงฎ PHP Functions
Estimated reading: 3 minutes 279 views

PHP Arrow Functions โ€“ Write Short, Elegant Functions in One Line

Learn how to use PHP arrow functions (fn) for concise, readable, and modern function expressions.


Introduction โ€“ What Are Arrow Functions in PHP?

Arrow functions are shorter alternatives to anonymous functions introduced in PHP 7.4. They are ideal for simple, single-expression callbacks like mapping, filtering, and inline calculations.

In this guide, youโ€™ll learn:

  • The syntax and usage of arrow functions (fn)
  • How they compare to traditional anonymous functions
  • Real-world use cases like array_map() and array_filter()
  • Best practices for clean and efficient code

1. Basic Arrow Function Syntax

$greet = fn($name) => "Hello, $name!";

echo $greet("Alice"); // Hello, Alice!

Arrow functions use the fn keyword with => instead of return.
Perfect for one-liner functions and quick return values.


2. Auto-Capturing Variables

$prefix = "Welcome, ";

$greet = fn($name) => $prefix . $name;

echo $greet("Bob"); // Welcome, Bob

Arrow functions automatically capture outer variables by value.
No need for use() like in closures.


3. Arrow Function in array_map()

$nums = [1, 2, 3];

$squares = array_map(fn($n) => $n * $n, $nums);

print_r($squares); // [1, 4, 9]

Makes array functions cleaner and easier to read.
Common in data transformation, filtering, and sorting.


4. Multiple Parameters

$add = fn($a, $b) => $a + $b;

echo $add(3, 7); // 10

Supports multiple inputs just like regular functions.
Great for inline math or logic operations.


5. Arrow Functions Cannot Use Statements

//  This won't work
$invalid = fn($x) => { return $x * 2; }; // Syntax error

Only a single expression is allowed, no {} blocks or multiple lines.
For complex logic, use anonymous or named functions.


Arrow vs Anonymous Functions

FeatureArrow Function (fn)Anonymous Function (function)
SyntaxShort, single expressionVerbose, supports full blocks
Variable captureAutomaticManual via use()
Multiline logic Not supported Supported
PHP versionPHP 7.4+PHP 5.3+

Best Practices

  • Use arrow functions for short, pure, and inline logic
  • Leverage automatic variable capture for cleaner code
  • Avoid arrow functions for multi-line or side-effect-heavy tasks
  • Prefer arrow functions in array_map(), array_filter(), and callbacks

Summary โ€“ Recap & Next Steps

PHP arrow functions help write clean, modern, and efficient code โ€” especially when used for functional-style programming and inline expressions.

Key Takeaways:

  • Arrow functions are concise one-liners introduced in PHP 7.4
  • Use fn($param) => expression format
  • Automatically inherit variables from outer scope
  • Ideal for callbacks, mapping, and single-return logic

Real-World Use Cases:
Data transformation, sorting, filtering, template rendering, utility wrappers.


Frequently Asked Questions (FAQs)

What is the minimum PHP version for arrow functions?
PHP 7.4.

Can arrow functions include multiple lines or statements?
No. Arrow functions only support a single expression.

Can arrow functions use variables from outside scope?
Yes. Variables are automatically captured by value.

When should I prefer arrow functions over anonymous functions?
When the logic is short, pure, and fits in one expression.

Can I return an arrow function from another function?
Yes. Arrow functions are just callable expressions like any other function.


Share Now :
Share

๐ŸŽฏ PHP Arrow Functions

Or Copy Link

CONTENTS
Scroll to Top