• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孙龙 程序员
少时总觉为人易,华年方知立业难
博客园    首页    新随笔    联系   管理    订阅  订阅
php回调函数 匿名函数 闭包函数

<?php

//回调函数1
function demo($num,$n){
for($i<0;$i<$num;$i++){
if($n($i))
continue;
echo $i."<br>";
}
}

function test1($i){
if($i%5 == 0)
return TRUE;
else
return FALSE;
}

demo(100,"test1");

//回调函数2 call_user_func_array
function demo($num,$n){
for($i<0;$i<$num;$i++){
if(call_user_func_array($n, array($i)))
continue;
echo $i."<br>";
}
}

function test1($i){
if($i%5 == 0)
return TRUE;
else
return FALSE;
}

demo(100,"test1");


//回调函数3
function demo($func){
echo $func();
}

demo(function(){
return "123456"
});

demo(function(){
return "99999"
});

//闭包
function demo(){
$a = 10;
$b = 20;
$one = function($str) use(&$a,&$b){
echo $str."<br>";
echo $b."<br>";
$a++;
echo $a."<br>";

};
return $one;
}

$var = demo();
$var("hello world1111");
$var("hello world2222");
$var("hello world3333");

/**
* 返回结果
hello world1111
20
11
hello world2222
20
12
hello world3333
20
13
*/







本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/8681438.html

posted on 2018-03-31 11:24  孙龙-程序员  阅读(152)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3