Skip to content

Instantly share code, notes, and snippets.

@a1exlism
Created June 27, 2016 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a1exlism/7ddaf3b3b207f5bef9579cf5ab97264c to your computer and use it in GitHub Desktop.
Save a1exlism/7ddaf3b3b207f5bef9579cf5ab97264c to your computer and use it in GitHub Desktop.
内部函数使用外部变量
function array_traverse($arr)
{
while (list($key, $value) = each($arr)) {
echo "$key => $value"."<br>";
}
}
$arr = array(1,2,3,4);
function array_powN ($arr, $n) {
return (array_map(function ($val) use($n){
// USE关键字, 可以在函数内使用函数之外的变量 JS就没这么多事...
return pow($val, $n);
}, $arr));
}
$arr = array_powN($arr, 3);
array_traverse($arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment