Why and how do you use anonymous functions in PHP?

前端 未结 6 1676
天命终不由人
天命终不由人 2020-11-27 16:14

Anonymous functions are available from PHP 5.3.
Should I use them or avoid them? If so, how?

Edited; just found some nice trick with php anonymo

6条回答
  •  被撕碎了的回忆
    2020-11-27 16:27

    Here is sample example of using using anonymous functions in Php

    $suppliers=['add1'=>'nodia','add2'=>'delhi', 'add3'=>'UP'];
     $getAddress = function($suppliers){ $address=[];
     for($i=1;$i<5;$i++){
      if(array_key_exists('add'.$i, $suppliers))
        $address[]=$suppliers['add'.$i];
      }
     return $address;};
     print_r($getAddress($suppliers));
    

    In above example we have written anonymous functions that check if a key exists in inputted array. If it exists then it will put that into output array.

提交回复
热议问题