The content of this article is to share with you a detailed explanation of anonymous functions and closure functions in PHP. It has a certain reference value. Friends in need can refer to
#tags: Anonymous function closure function php closure function php anonymous function function use
##Introduction: Anonymous functions and closure functions are not particularly advanced knowledge, but many friends who are just getting started are always confused, because everyone is accustomed to writing functions just for calling them. What are anonymous functions and closure functions used for?Anonymous functionsphp official explanation is as follows:
Anonymous functions (Anonymous functions), also called closure functions (closures), allow temporary creation of a function without a specified name . The value most commonly used as a callback function argument. Of course, there are other applications as well.
<?php echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world'); // 输出 helloWorld ?>
preg_replace_callback function It requires three parameters. The first parameter is a regular expression used to match data, the second parameter is a function, and the third parameter is the string that needs to be matched. You can also write it like the following
<?php $testFunc = function ($match) { return strtoupper($match[1]); }; echo preg_replace_callback('~-([a-z])~', $testFunc, 'hello-world'); // 输出 helloWorld ?>
Learn Javascript closure (Closure) - Ruan Yifeng
<?php function a(){ $a = 11; function b(){ $b = 22; echo $a; echo $b; } b(); } a(); //报Notice:Undefined variable: a in index.php on line 6 //22 ?>
<?php $count = 0; $a = function() { var_dump($count); }; $b = function() use ($count) { var_dump($count); }; $count++; $c = function() use (&$count) { var_dump($count); }; $count++; $a(); // null Notice: Undefined variable: count in $b(); // int 0 $c(); // int 2 $count++; $b(); // int 0 ?>
Analysis: The closure function (anonymous function) uses use to obtain the side effect domain variable when the function is defined, no matter when it is called. If you want to get the variable value when calling, you need to pass it by reference. How to use it depends on the usage scenario.
Lists several common scenariosAs a callback function
//一个我们使用过的例子 <?php/* * 菜谱拆分食物后的拼接 * 参数均不能为空 * */public function mergeFoodsStr($str,array $mapping){ // $str = '白菜半棵、__2__鲍菇两只、__0__一根,__1__两根,三者比例为100:100:15,酱油5克,香油2克,盐1克。';// $mapping = array(// 0 =>array('name' => '胡萝卜','id' => '81' ),// 1 =>array ( 'name' => '萝卜', 'id' => '72'),// 2 =>array ( 'name' => '杏', 'id' => '1841')// ); if(empty($str) || empty($mapping)){ return false; } $strNew = preg_replace_callback('"|__(\d)__|" ',function ($matches) use ($mapping){ return $mapping[$matches[1]]['name']; },$str); $this->log('拼接后的食材字符串',$strNew); return $strNew; }?>
The above is the detailed content of Detailed explanation of anonymous functions and closure functions in PHP. For more information, please follow other related articles on the PHP Chinese website!