PHP Arrow Functions Best Tutorial 2025

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 {} और return keyword की जरूरत नहीं होती।
  • 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
image 9

Without Parameters:

<?php
  $sayHello = fn() => "Hello, World!";
  echo $sayHello(); 
  
  // Output: Hello, World!
?>
PHP

Single Parameters:

<?php
  $square = fn($n) => $n * $n;
  echo $square(5); 
  
  // Output: 25
?>
PHP

Php 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
?>
PHP

Note: Anonymous Functions में use keyword की जरूरत होती है, जबकि Arrow Functions में यह स्वतः उपलब्ध होता है।

Php Arrow Functions vs. Anonymous Functions

ParameterArrow FunctionAnonymous Functions
Syntaxछोटा और सरलथोड़ा लंबा और complex
Scope HandlingOuter 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

ParameterArrow FunctionRegular Functions
SyntaxSimple और compactज्यादा detailed
Multiple StatementsNot AllowedAllowed
Use Caseछोटे tasks के लिएबड़े और complex tasks के लिए
Scope HandlingOuter scope को directly access करता हैuse या global keyword का उपयोग होता है।

Conclusion:

PHP Arrow Functions छोटे और simple फंक्शन्स के लिए बहुत उपयोगी हैं। ये Anonymous Functions का बेहतर और compact रूप हैं।

Arrow Functions का उपयोग तब करें:

  • जब फंक्शन simple और एक expression वाला हो।
  • जब callback या closures की जरूरत हो।
error: Content is protected!! You are not allowed to copy.