php的闭包

闭包函数:临时创建一个没有名称的函数,经常作为回调函数来用。通俗的说就是:子函数可以使用父函数中的局部变量,这种行为叫做闭包。

1、匿名函数赋值

$demo=function($str){

   echo $str;

 }
 $demo('hello,world');

2、闭包可以从父作用域中继承变量,任何此类型变量都应该用use语言结构传递进去。

$message='hello';

 $example=function() use ($message){

   var_dump($message);

 };

 echo $example();

结果:hello;

$example=function() use (&$message){

    var_dump($message);

  }

结果:hello;

$message='world';

  echo $example();

结果:world;

$example=function($arg) use ($message){

    var_dump($arg.' '.$message);

  }

  $example('hello');

结果:hello world;

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_1681.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?