# php匿名函数

**URL:** <https://www.shiqidu.com/t/topic/368>\
**Category:** 开发调优\
**Tags:** PHP\
**Created:** [2018 年2 月 5 日 04:01 UTC](https://www.shiqidu.com/t/topic/368 "2018-02-05T04:01:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![dajiang](https://www.shiqidu.com/user_avatar/www.shiqidu.com/dajiang/32/468_2.png) [@dajiang](https://www.shiqidu.com/u/dajiang)\
发布日期： [2018 年2 月 5 日 04:01 UTC](https://www.shiqidu.com/t/topic/368/1 "2018-02-05T04:01:28Z")

</div>

在创建闭包时, 生成所use变量的快照, 下文再次调用闭包函数时, 快照变量不改变

```php
$a = 5;    
$b = function ($x) use ($a) {    
$a += $x; echo $a;    
};    
$a = 10; // 这个变量被再次赋值, 但是在use语句中的'快照'是不会改变的    
$b(100); // 输出 : 105    

```

实际应用 2 : 使用引用传值, 生成所use变量的指针, 下文再次调用闭包函数时, 快照变量会改变(其实这样做与直接传参已经没有区别, 所以这么做意义不大, 而且代码可读性降低)

```php
$a = 5;    
$b = function ($x) use (&amp;$a) {    
$a += $x; echo $a;    
};    
$a = 10; // 变量重新赋值, 上文中闭包所引用的变量值也被改变    
$b(100); // 输出 : 110    

```

\> 引自：[php 使用use 和直接传参的区别\_百度知道](https://zhidao.baidu.com/question/181444410959366364.html)
