codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

17 / 56 topics
14Functions in PHP15User-Defined Functions16Anonymous Functions17Arrow Functions18Recursion in PHP
Tutorials/PHP/Arrow Functions
🐘PHP

Arrow Functions

Updated 2026-05-15
10 min read

Arrow Functions

Introduction

In PHP, arrow functions provide a concise syntax for creating anonymous functions. Introduced in PHP 7.4, they offer a more streamlined approach compared to the traditional function keyword. This tutorial will guide you through understanding and using arrow functions effectively.

Concept

Arrow functions are particularly useful when working with higher-order functions like array_map, array_filter, or array_reduce. They allow for cleaner and more readable code by reducing boilerplate syntax.

Syntax

The basic syntax of an arrow function is:

$functionName = fn($parameter1, $parameter2, ...) => expression;
  • fn: This keyword introduces the arrow function.
  • Parameters: The parameters are listed within parentheses, similar to regular functions.
  • Arrow (=>): This separates the parameter list from the return expression.
  • Expression: The expression after the arrow is automatically returned.

Key Characteristics

  1. Implicit Return: Arrow functions automatically return the result of the expression following the arrow. There's no need for an explicit return statement unless you want to return multiple expressions or use control structures like if.

  2. Lexical $this Binding: Arrow functions inherit the value of $this from the parent scope, unlike traditional anonymous functions which have their own $this context.

  3. Shorter Syntax: They provide a shorter syntax compared to regular anonymous functions, making code more concise and easier to read.

Examples

Basic Usage

Let's start with a simple example where we use an arrow function to square each element in an array:

$numbers = [1, 2, 3, 4, 5];
$squaredNumbers = array_map(fn($num) => $num * $num, $numbers);

print_r($squaredNumbers);
Output
Array
(
  [0] => 1
  [1] => 4
  [2] => 9
  [3] => 16
  [4] => 25
)

In this example, fn($num) => $num * $num is an arrow function that takes a number and returns its square. The array_map function applies this arrow function to each element of the $numbers array.

Multiple Parameters

Arrow functions can also take multiple parameters:

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

echo $result; // Outputs: 8
Output
8

Here, the arrow function fn($a, $b) => $a + $b takes two parameters and returns their sum.

Lexical $this Binding

Arrow functions inherit the value of $this from the parent scope. This is particularly useful in object-oriented programming:

class Calculator {
    private $value = 0;

    public function add($num) {
        return fn($n) => $this->value += $n;
    }
}

$calc = new Calculator();
$addOne = $calc->add(1);
$addTwo = $calc->add(2);

$addOne(5); // Adds 5 to the value
$addTwo(3); // Adds 3 to the value

echo $calc->value; // Outputs: 8
Output
8

In this example, the arrow function fn($n) => $this->value += $n captures the $this context of the Calculator class. When called, it modifies the private property $value.

Returning Multiple Expressions

If you need to return multiple expressions, you can use curly braces and an explicit return statement:

$greet = fn($name) => {
    $message = "Hello, " . $name;
    return $message;
};

echo $greet("Alice"); // Outputs: Hello, Alice
Output
Hello, Alice

Here, the arrow function uses curly braces to enclose multiple statements. The return keyword is used to specify the value to be returned.

What's Next?

Now that you have a solid understanding of arrow functions in PHP, you might want to explore more advanced topics such as recursion. Recursion involves a function calling itself, which can be a powerful technique for solving problems like traversing data structures or implementing algorithms.

Stay tuned for the next section where we dive deeper into recursion and how it can be applied in PHP!


PreviousAnonymous FunctionsNext Recursion in PHP

Recommended Gear

Anonymous FunctionsRecursion in PHP