Menu
Coddy logo textTech

Arrow Functions

Part of the Logic & Flow section of Coddy's PHP journey — lesson 3 of 68.

Arrow functions, introduced in PHP 7.4, provide a more concise way to write simple anonymous functions. They use the fn keyword instead of the full function syntax, making your code shorter and cleaner.

Here's how you create an arrow function:

<?php
$greeting = fn($name) => "Hello, " . $name . "!";
echo $greeting("Alice");  // Outputs: Hello, Alice!
?>

Notice the key differences: arrow functions use fn instead of function, have an arrow => pointing to the expression, and don't need curly braces or a return statement for simple expressions.

The biggest advantage of arrow functions is that they automatically capture variables from the parent scope - no use keyword needed:

<?php
$message = "Welcome";
$greeting = fn($name) => $message . ", " . $name . "!";
echo $greeting("Bob");  // Outputs: Welcome, Bob!
?>

Arrow functions are perfect for short, simple operations where the traditional anonymous function syntax feels too verbose.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create an arrow function that takes a product name as a parameter and returns a formatted product label. The arrow function should capture a category name from the surrounding scope.

You will receive two inputs: the category name and the product name. Read both inputs, create an arrow function that captures the category and formats the product label as "[Category] Product", call the arrow function with the product name, and print the result.

Input format: Two lines - first line contains the category name, second line contains the product name

Expected output: The formatted product label in the format "[Category] Product"

Cheat sheet

Arrow functions use the fn keyword and provide a concise syntax for simple anonymous functions:

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

Key features:

  • Use fn instead of function
  • Arrow => points to the expression
  • No curly braces or return statement needed for simple expressions
  • Automatically capture variables from parent scope without use keyword
$message = "Welcome";
$greeting = fn($name) => $message . ", " . $name . "!";
echo $greeting("Bob");  // Outputs: Welcome, Bob!

Try it yourself

<?php
// Read input
$category = trim(fgets(STDIN));
$product = trim(fgets(STDIN));

// TODO: Write your code below
// Create an arrow function that captures the category and formats the product label


// Call the arrow function and output the result
echo $result;
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow