赋予函数对外部变量的访问权限我在外面有一个数组:$myArr = array();我想让我的函数访问它之外的数组,这样它就可以为它添加值。function someFuntion(){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;}如何对变量赋予函数正确的作用域?
3 回答
达令说
TA贡献1821条经验 获得超6个赞
$foo = 42;$bar = function($x = 0) use ($foo){ return $x + $foo;};var_dump($bar(10)); // int(52)
牧羊人nacy
TA贡献1862条经验 获得超7个赞
Global $myArr;$myArr = array();function someFuntion(){ global $myArr; $myVal = //some processing here to determine value of $myVal $myArr[] = $myVal;}
function someFuntion($myArr){ $myVal = //some processing here to determine value of $myVal $myArr[] = $myVal; return $myArr;}$myArr = someFunction($myArr);
- 3 回答
- 0 关注
- 401 浏览
添加回答
举报
0/150
提交
取消