You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
787 B

  1. <?php
  2. //定义匿名函数
  3. $a = function(){
  4. echo 'hello'.'<br>';
  5. // return 'hello';
  6. };
  7. $a();
  8. //查看变量内容
  9. var_dump(($a));
  10. //闭包=匿名函数
  11. //函数里面的函数
  12. function func(){
  13. //定义局部变量
  14. $b='world';
  15. $innerFunc = function(){
  16. //函数内部的函数
  17. //调用函数func的局部变量
  18. echo $b.'<br>';
  19. };
  20. //调用匿名函数
  21. $innerFunc();
  22. }
  23. func();
  24. // echo $a().'<br>';
  25. // //匿名函数作为参数,回调函数
  26. // $arr=[1,2,3,4];
  27. // $ret=array_map(function ($num){
  28. // return $num*$num;
  29. // }, $arr);
  30. // echo '<pre>';
  31. // print_r($ret);
  32. ?>