php回调函数 匿名函数 闭包函数
<?php
//回调函数1
function demo($num,$n){
for($i<0;$i<$num;$i++){
if($n($i))
continue;
echo $i."
";
}
}
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."
";
}
}
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."
";
echo $b."
";
$a++;
echo $a."
";
};
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
*/