返回php函数与闭......登陆

php函数与闭包(匿名函数)

温度。2019-05-21 14:28:28186

<?php


$demo1=function($name){

return $name.'最爱的人是:赵敏';

};


echo $demo1('张无忌'),'<hr>';


$belle='周芷若';


$demo2=function($name){

return '张无忌最爱的人是:'.$name;

};

echo $demo2($belle),'<hr>';



$func1=function (){

$name='无忌哥哥';


$test=function() use ($name){

       return $name.',我被另一个函数包住了,快来救我';

};


return $test();

};


echo $func1(),'<hr>';



$name='无忌哥哥';


$test=function() use ($name){

       return $name.',我又被当做参数了,你还要我吗';

};


$func2=function (callable $test){


return $test();

};


echo $func2($test),'<hr>';



$func3=function (){

$name='无忌哥哥';

//在函数中声明一个匿名函数

$test=function() use ($name){

       return $name.',我又被当做返回值了';

};

);

return $test;

};




echo $func3()(),'<hr>';


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送