如何从匿名函数访问外部变量?

时间:2015-02-05 20:14:19

标签: php

这是我的功能

function sample($greet, $otherVar) {

   $codigo = preg_replace_callback("/(**_(\w+))/",

      function($matches) use ($greet) {

          if($matches[1] == $otherVar) { 

            (...)

重点是..我的主函数接收 $ otherVar 作为参数..我想在匿名函数中使用这个值..

1 个答案:

答案 0 :(得分:4)

您可以通过use关键字执行此操作,就像您已为$greet变量所做的那样。像这样:

function($matches) use ($greet, $otherVar) {
相关问题