← Back to PHP Course | Chapter 5: Functions | Lesson 8 of 10

PHP Arrow Functions

एक arrow function एक बहुत छोटा one-line helper लिखने का तरीका है। यह एक function का text-message version जैसा है, और यह automatically अपने आसपास के variables झाँक लेता है।
Syntax
php
$closure = fn($parameter) => expression;   // PHP 7.4+, captures outer variables automatically

$result = array_map(fn($item) => expression, $array);

Arrow Functions क्या हैं? (PHP 7.4+)

PHP 7.4 में introduce किए गए, arrow functions fn keyword इस्तेमाल करते हैं और एक single expression तक limited हैं, जिसका result automatically return होता है — कोई explicit return keyword नहीं, और कोई curly-brace body नहीं, यही वजह है कि वे simple cases के लिए एक standard anonymous function से इतने ज़्यादा छोटे हैं।

उदाहरण: What are Arrow Functions? (PHP 7.4+)

php
<?php
// Declare `$double`, set to `fn($x) => $x * 2`
$double = fn($x) => $x * 2;
// Print `$double(5)` to the output
echo $double(5);
?>

Implicit Value Binding

एक regular anonymous function के उलट, एक arrow function अपनी आसपास की scope से किसी भी ऐसे variable को automatically capture करता है जिसे यह actually reference करता है — value से — बिना किसी use clause की ज़रूरत के।

यह implicit capture ही एक standard closure से main practical अंतर है।

उदाहरण: Implicit Value Binding

php
<?php
// Declare `$tax`, set to `0.1`
$tax = 0.1;
// Declare `$addTax`, set to `fn($price) => $price + ($price * $tax)`
$addTax = fn($price) => $price + ($price * $tax);
// Print `$addTax(100)` to the output
echo $addTax(100);
?>

Callbacks की तरह Arrow Functions

क्योंकि arrow functions इतने compact हैं, वे array_map() या usort() जैसी किसी चीज़ के लिए एक inline argument की तरह naturally पढ़ते हैं — पूरा callback उसी line में fit हो जाता है जिस function call को इसकी ज़रूरत है, ऊपर एक अलग multi-line closure definition के बिना।

उदाहरण: Arrow Functions as Callbacks

php
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4]`
$numbers = [1, 2, 3, 4];
// Declare `$doubled`, set to `array_map(fn($n) => $n * 2, $numbers)`
$doubled = array_map(fn($n) => $n * 2, $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>

Arrow Functions को Type करना

आप एक arrow function में parameter और return type declarations exactly वैसे ही add कर सकते हैं जैसे किसी दूसरे function में करेंगे — fn(int $x): int => $x * 2 — इसलिए syntax का brevity type safety छोड़ने की कीमत पर नहीं आता।

उदाहरण: Typing Arrow Functions

php
<?php
// Declare `$double`, set to `fn(int $x): int => $x * 2`
$double = fn(int $x): int => $x * 2;
// Print `$double(5)` to the output
echo $double(5);
?>

Arrow Functions बनाम Anonymous Functions

एक arrow function की compactness का tradeoff यह है कि यह सिर्फ एक single expression रख सकता है — कोई loops नहीं, कई statements नहीं, कोई intermediate variables नहीं।

उस single expression से आगे किसी भी चीज़ के लिए इसके बजाय एक standard anonymous function पर वापस जाना पड़ता है।

उदाहरण: Arrow Functions vs Anonymous Functions

php
<?php
$sum = fn($a, $b) => $a + $b;
echo $sum(2, 3);
// A multi-statement version would need a regular function instead
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक arrow function body में कई statements लिखने की कोशिश करना, जबकि fn सिर्फ एक expression की अनुमति देता है।
  2. यह उम्मीद करना कि एक arrow function एक outer variable बदल देगा, जैसे fn() => $count++, जबकि यह value से capture करता है इसलिए outer variable same रहता है।
  3. PHP 7.4 से पहले के versions पर fn इस्तेमाल करना, जो एक parse error से fail हो जाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.