Share your knowledge with other learners . . . Write As Guest

PHP Anonymous Functions In Hindi

📔 : PHP 🔗

Anonymous Functions , जैसा कि नाम से ही ही मालूम होता है ऐसे functions जिनका कोई name नहीं है , Yes PHP हमें ये facility provide करती है कि हम without name के functions भी define कर सके।   Anonymous Functions Closure Functions भी कहते हैं।


Anonymous Functions internally Closure class का use करते हैं।

PHP Anonymous Function Syntax

$var_ame = function()
{
  //write logic here
}

PHP Anonymous Function Example

File :anonymous_fun.php

Copy Fullscreen Close Fullscreen
<?php
  $var = function()
  {
    echo"Print Inside Anonymous Function";
  };
  $var();
?>
Output
Print Inside Anonymous Function

Note - Anonymous Functions define करते समय curly brackets के बाद semicolon ( ; ) लगाना न भूलें otherwise PHP Fatal Error Generate कर देगी।

Normal Functions की तरह ही Anonymous Functions में भी हम arguments pass कर सकते हैं। पिछले Topic में हमने Variable Length Arguments Functions के बारे में पढ़ा , उसी तरह से इसमें भी variable Length Arguments handle कर सकते हैं।

See Example

File : anonymous_fun2.php

Copy Fullscreen Close Fullscreen
<?php
  $my_fun = function()
  {
    echo"This is normal anonymous function.";
  };
  $my_fun();
  
  echo"<br>";
  /* function to handle variable list arguments */
  $list_fun = function(...$x)
  {
    echo"<pre>";    
    print_r($x);
  };
  $list_fun(23,45,67,889);
?>
Output
This is normal anonymous variable.
Array
(
    [0] => 23
    [1] => 45
    [2] => 67
    [3] => 889
)

PHP में Anonymous Functions को और किन - किन तरीकों से हम use कर सकते हैं -

  1. किसी external variable को function के अंदर use करने के लिए हम use keyword का use करते हैं।
  2. external variable  को use करने का दूसरा तरीका यह है कि function call करते समय वह variable pass करें।
  3. Function Call By Reference  की तरह इसमें आप parameter reference accept  करते समय value को modify  नहीं कर सकते है , इसके लिए आपको external variable को reference के साथ use() में pass करना पड़ेगा।

See Examples

File : anonymous_fun3.php

Copy Fullscreen Close Fullscreen
<?php
  $ex_var = 'External Variable';

  /* we can't access external variable directly */
  $fun = function()
  {
    echo var_dump($ex_var);
  };
  $fun();

  /* we can use like this */
  $fun = function() use($ex_var)
  {
    echo var_dump($ex_var);
  };
  $fun();

  /* we can do this */
  $fun = function($param) use($ex_var)
  {
    echo var_dump($ex_var." ".$ex_var);
  };
  $fun($ex_var);

  /* we can't modify variable value by passing the value */
  $fun = function(&$param)
  {
    $ex_var.=' modified';
    echo var_dump($ex_var);
  };
  $fun($ex_var);

  /* we can modify variable value using like this */
  $fun = function() use(&$ex_var)
  {
    $ex_var.=' modified';
    echo var_dump($ex_var);
  };
  $fun($ex_var);

  echo'After Modify variable value : '.$ex_var;
?>
Output
NULL
string(17) "External Variable"
string(35) "External Variable External Variable"
string(9) " modified"
string(26) "External Variable modified"
After Modify variable value : External Variable modified

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers