Access a global variable in a PHP function

前端 未结 9 1990
感动是毒
感动是毒 2020-11-29 05:33

According to the most programming languages scope rules, I can access variables that are defined outside of functions inside them, but why doesn\'t this code work?



        
9条回答
  •  粉色の甜心
    2020-11-29 06:09

    Another way to do it:

    UPDATE 2020-01-13: requested by Peter Mortensen

    As of PHP 5.3.0 we have anonymous functions support that can create closures. A closure can access the variable which is created outside of its scope.

    In the example, the closure is able to access $data because it was declared in the use clause.

提交回复
热议问题