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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate 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
fninstead offunction - Arrow
=>points to the expression - No curly braces or
returnstatement needed for simple expressions - Automatically capture variables from parent scope without
usekeyword
$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;
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting