PHP Arrow Functions
In this page:
$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
// Declare `$double`, set to `fn($x) => $x * 2`
$double = fn($x) => $x * 2;
// Print `$double(5)` to the output
echo $double(5);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
$sum = fn($a, $b) => $a + $b;
echo $sum(2, 3);
// A multi-statement version would need a regular function instead
?>
Login to try C/C++/Java/PHP code in the editor
- एक arrow function body में कई statements लिखने की कोशिश करना, जबकि
fnसिर्फ एक expression की अनुमति देता है। - यह उम्मीद करना कि एक arrow function एक outer variable बदल देगा, जैसे
fn() => $count++, जबकि यह value से capture करता है इसलिए outer variable same रहता है। - PHP 7.4 से पहले के versions पर
fnइस्तेमाल करना, जो एक parse error से fail हो जाता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: