Home >Backend Development >PHP Tutorial >Learn closures in PHP 5.3: function() use(&$param)

Learn closures in PHP 5.3: function() use(&$param)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:07:421187browse
  1. function closureCreater(){

  2. $x =1;
  3. return function($fun=null) use(&$x){//Pass value by reference
  4. echo "
    ".$x++;
  5. $fun and $fun();
  6. };
  7. }

  8. $x = "hello world";

  9. $test = closureCreater( );
  10. $test();
  11. $test(function(){ echo "closure test one"; });
  12. $test(function(){ echo "closure test two"; });
  13. $test(function( ) use($x){ echo "
    ".$x;});
  14. //Save the function as an array element
  15. $x = 'outer param.';
  16. $arr = array() ;
  17. $arr[] = function($str)use($x){ return $str.$x; };
  18. echo $arr[0]('test fun in arr,'); //test fun in arr ,outer param.
  19. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn