PHP 中的闭包函数和匿名函数
发布时间:2022-08-02 14:07:36 所属栏目:PHP教程 来源:互联网
导读:闭包函数 闭包函数通常作为函数中的函数使用。 ?php $foo = function($s) { echo $s; }; $foo(hello); ?php function test() { $a = 1; $b = 2; $foo = function($s) use($a, $b) { echo $s . ($a + $b); }; $foo(hello); } test(); ?php // 返回一个闭包函
|
闭包函数 闭包函数通常作为函数中的函数使用。 <?php $foo = function($s) { echo $s; }; $foo('hello'); <?php function test() { $a = 1; $b = 2; $foo = function($s) use($a, $b) { echo $s . ($a + $b); }; $foo('hello'); } test(); <?php // 返回一个闭包函数供外部调用 function test() { $foo = function($s) { echo $s; }; return $foo; } $res = test(); $res('hello') 匿名函数 匿名函数通常作为回调函数的参数使用。 function foo($callback){ return $callback(); } $str1 = "hello"; $str2 = "world"; foo(function() use($str1, $str2) { // 传入一个匿名函数作为参数 echo $str1 . " " . $str2; return true; }); ![]() (编辑:三门峡站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |



