PHP Arrow Functions, PHP 7.4 में जोड़ी गई एक नई सुविधा है। यह गुमनाम फंक्शन (Anonymous Function) का सरल और छोटा रूप है। Arrow Functions का उपयोग छोटे और सरल फंक्शन्स को define करने के लिए किया जाता है।
Arrow Functions को लैम्ब्डा फंक्शन (Lambda Functions) भी कहा जाता है। ये syntax को आसान और readable बनाते हैं।
PHP Arrow Functions का Syntax:
fn (parameters) => expression;
fnकीवर्ड का उपयोग Arrow Functions को define करने के लिए किया जाता है।- इसमें curly braces
{}औरreturnkeyword की जरूरत नहीं होती। - Php Arrow Functions में हमेशा एक ही expression होता है, जिसका परिणाम (result) return होता Example है।
Example Of PHP Arrow Functions:
Basic Example:
<?php
$add = fn($a, $b) => $a + $b;
echo $add(10, 20); // Output: 30
?>PHP
Without Parameters:
<?php
$sayHello = fn() => "Hello, World!";
echo $sayHello();
// Output: Hello, World!
?>PHPSingle Parameters:
<?php
$square = fn($n) => $n * $n;
echo $square(5);
// Output: 25
?>PHPPhp Arrow Functions में use का उपयोग:
Php Arrow Functions को बाहर के scope से variables को access करने के लिए use की आवश्यकता नहीं होती। ये closure की तरह ही बाहरी scope से variables को directly access कर सकते हैं।
<?php
$factor = 2;
$multiply = fn($n) => $n * $factor;
echo $multiply(5);
// Output: 10
?>PHPNote: Anonymous Functions में use keyword की जरूरत होती है, जबकि Arrow Functions में यह स्वतः उपलब्ध होता है।
Php Arrow Functions vs. Anonymous Functions
| Parameter | Arrow Function | Anonymous Functions |
|---|---|---|
| Syntax | छोटा और सरल | थोड़ा लंबा और complex |
| Scope Handling | Outer scope को सीधे access कर सकते हैं। | use keyword की जरूरत होती है। |
| Expression-Based | केवल एक ही expression return कर सकते हैं। | Multiple statements के लिए उपयोगी। |
| Readability | कोड पढ़ने में आसान | कोड थोड़ा कठिन हो सकता है। |
Advantages of Php Arrow Functions?
- Short Syntax: Syntax छोटा और आसान होता है।
- Readable Code: Arrow Functions को पढ़ना और समझना आसान होता है।
- Better Scope Handling: Outer scope के variables को सीधे access कर सकते हैं।
- Efficient for Simple Tasks: छोटे और temporary tasks के लिए उपयोगी।
Disadvantages of Arrow Functions?
- Single Expression Limit: केवल एक expression को return कर सकते हैं, complex logic के लिए उपयुक्त नहीं।
- Limited Flexibility: Anonymous Functions की तुलना में कम flexible।
- Beginner-Friendly नहीं: नए developers को समझने में कठिनाई हो सकती है।
Comparison: Php Arrow Functions vs Regular Functions
| Parameter | Arrow Function | Regular Functions |
|---|---|---|
| Syntax | Simple और compact | ज्यादा detailed |
| Multiple Statements | Not Allowed | Allowed |
| Use Case | छोटे tasks के लिए | बड़े और complex tasks के लिए |
| Scope Handling | Outer scope को directly access करता है | use या global keyword का उपयोग होता है। |
Conclusion:
PHP Arrow Functions छोटे और simple फंक्शन्स के लिए बहुत उपयोगी हैं। ये Anonymous Functions का बेहतर और compact रूप हैं।
Arrow Functions का उपयोग तब करें:
- जब फंक्शन simple और एक expression वाला हो।
- जब callback या closures की जरूरत हो।

Welcome to HindiTutorials, your go-to platform for learning and mastering concepts in Hindi! Our mission is to make education accessible and simple for everyone by providing high-quality tutorials on programming, technology, and various other topics.