闭包函数

闭包函数通常作为函数中的函数使用。

<?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;
});

最新文章

  1. NOIP2012国王游戏
  2. C++的一些小的知识点
  3. Python模块(radom)
  4. JDBC getMetaData将结果集组装到List
  5. JAVA类与对象(六)------实例变量与类变量的区别,实例方法和类方法的区别
  6. lintcode :Reverse Words in a String 翻转字符串
  7. python【第十一篇】消息队列RabbitMQ、缓存数据库Redis
  8. [转] C++指针加整数、两个指针相减的问题
  9. 【学习笔记01】:hover为DIV添加鼠标悬停时改变颜色的效果
  10. 基于KVM建立虚拟机的步骤及总结说明
  11. el表达式跟ognl表达式的区别
  12. org.apache.jasper.JasperException: - Page directive must not have multiple occurrences of pageencoding
  13. thinkphp5.0无限极分类及格式化输出
  14. 视觉SLAM中相机详解
  15. 关于Node.js中的路径问题
  16. pandas处理时间序列(4): 移动窗口函数
  17. xib中的label加边框
  18. vue教程2-07 自定义指令
  19. MT【186】四边形中的余弦定理
  20. jsp内置对象request 和response

热门文章

  1. 【Linux】【Services】【SaaS】Docker+kubernetes(7. 安装Docker私有镜像仓库)
  2. ClassLoader.loadClass()与Class.forName()的区别《 转》
  3. Vector Bin Packing 华为讲座笔记
  4. VSCode上发布第一篇博客
  5. PHP安装sqlsrv扩展( Centos系统、或宝塔面板)
  6. 一个使用 asyncio 开发的网络爬虫(译文)
  7. 修复 Edge 浏览器 1Password 插件 Ctrl+Shift+X 弹出快捷键失效
  8. 突出显示(Project)
  9. CF513B1 Permutations 题解
  10. CF701A Cards 题解