【发布时间】:2020-04-26 12:07:17
【问题描述】:
我在外面有一个数组:
$myArr = array();
我想让我的函数访问它外部的数组,以便它可以向它添加值
function someFuntion(){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}
如何为函数赋予变量正确的作用域?
【问题讨论】:
我在外面有一个数组:
$myArr = array();
我想让我的函数访问它外部的数组,以便它可以向它添加值
function someFuntion(){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}
如何为函数赋予变量正确的作用域?
【问题讨论】:
默认情况下,当您在函数内部时,您无权访问外部变量。
如果您希望您的函数能够访问外部变量,则必须在函数内部将其声明为 global:
function someFuntion(){
global $myArr;
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}
更多信息,请参阅Variable scope。
但请注意,使用全局变量不是一个好习惯:这样,您的函数就不再独立了。
一个更好的主意是让你的函数返回结果:
function someFuntion(){
$myArr = array(); // At first, you have an empty array
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal; // Put that $myVal into the array
return $myArr;
}
然后像这样调用函数:
$result = someFunction();
您的函数也可以接受参数,甚至处理通过引用传递的参数:
function someFuntion(array & $myArr){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal; // Put that $myVal into the array
}
然后,像这样调用函数:
$myArr = array( ... );
someFunction($myArr); // The function will receive $myArr, and modify it
有了这个:
有关这方面的更多信息,您应该阅读 PHP 手册的 Functions 部分,尤其是以下子部分:
【讨论】:
global 方法引用全局变量,而不是外部变量!
$foo = 42;
$bar = function($x = 0) use ($foo){
return $x + $foo;
};
var_dump($bar(10)); // int(52)
更新:现在支持箭头功能,但我会让更多使用它的人来创建答案
【讨论】:
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);
这样你就不用依赖 Globals了。
【讨论】:
$myArr = array();
function someFuntion(array $myArr) {
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
return $myArr;
}
$myArr = someFunction($myArr);
【讨论】:
使用全局变量是实现目标的一种可能不太好的方法。
您可以通过在函数开头添加global $myArr; 来实现。
但是请注意,在大多数情况下,使用全局变量是一个坏主意,并且可能是可以避免的。
更好的方法是将数组作为参数传递给函数:
function someFuntion($arr){
$myVal = //some processing here to determine value of $myVal
$arr[] = $myVal;
return $arr;
}
$myArr = someFunction($myArr);
【讨论】:
这真的是关于事情的正确顺序。
<?php
/*In general(the rule can be broken) code is interpreted left to right
top to bottom.
If you want a function to be able to use the values you input,
write the function first. This means the function should be above where
it is requested in the code. Add some parameters($param). Note it does
not need to be called $param, I use $value in the example. This can be
multiple $vars going from left to right i.e($param_1,$param_2), or be an
array(), or a mix. Just remember left to right. Left values must exist
before right values.*/
//Example function here
function foo($value){
return $value[0] + 1;
}
//Optional way to create array
//$value[0] = 0;
$value = array(0);
$limit = 10;
while($value[0] < $limit){
//Request the function here as many times as you want
echo $value[0] = foo($value);
echo "<br>";
}
//Clean up afterwards
unset($value,$limit);
?>
【讨论】: