返回

php回调函数 匿名函数 闭包函数

发布时间:2022-10-21 09:05:00 329
# 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

*/

 

 

 

 

 

 

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
回调函数的原理及PHP实例 2022-10-21 08:36:24