note 126217 deleted from language.variables.scope by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:32:04 +0000 |
| Subject: | note 126217 deleted from language.variables.scope by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217912@lists.php.net to get a copy of this message | ||
Note Submitter: admin at shenjinpeng dot cn
----
<?php
function foo(){
static $a= 0;
$bar = function(){
global $a ;
return ++$a;
};
return $bar();
}
echo foo(); // 1
echo foo(); // 2
function foo1(){
$bar = function(){
static $a = 0;
return ++$a;
};
return $bar();
}
echo foo1() ; // 1
echo foo1() ; // 1
function foo2(){
static $a = 0;
$bar = function() use (&$a){
return ++$a;
};
return $bar();
}
echo foo2() ; // 1
echo foo2() ; // 2
function foo3($func){
static $a = 0;
return $func($a);
}
echo foo3(fn(&$a) => ++$a ); // 1
echo foo3(fn(&$a) => ++$a ); // 2
?>