让我的函数访问外部变量

Giving my function access to outside variable

提问人:brett 提问时间:3/28/2010 更新时间:12/13/2022 访问量:144466

问:

我在外面有一个数组:

$myArr = array();

我想让我的函数访问它外面的数组,以便它可以向它添加值

function someFuntion(){
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;
}

如何为函数提供变量的正确范围?

php 函数 范围

评论


答:

9赞 Amy B 3/28/2010 #1
$myArr = array();

function someFuntion(array $myArr) {
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;

    return $myArr;
}

$myArr = someFunction($myArr);

评论

2赞 mickmackusa 5/6/2020
如果对片段进行解释,则此答案的质量将大大提高。
0赞 alfianrehanusa 8/9/2022
将变量传递给参数函数,很棒
4赞 lamas 3/28/2010 #2

实现目标的一种可能不太好的方法是使用全局变量。

您可以通过添加到函数的开头来实现这一点。 但是,请注意,在大多数情况下,使用全局变量是一个坏主意,并且可能是可以避免的。global $myArr;

更好的方法是将数组作为参数传递给函数:

function someFuntion($arr){
    $myVal = //some processing here to determine value of $myVal
    $arr[] = $myVal;
    return $arr;
}

$myArr = someFunction($myArr);
13赞 Tyler Carter 3/28/2010 #3
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);

这样你就不用依赖全局了。

评论

0赞 svens 3/28/2010
函数范围内的全局就足够了,您是否故意在“主”范围内添加了一个?好的做法?(从不使用全局变量..)
0赞 andreszs 10/24/2014
函数外部的“全局”是无用的,除非整个文件是从另一个函数中包含的。
152赞 Pascal MARTIN 3/28/2010 #4

默认情况下,当您在函数内部时,您无权访问外部变量。


如果你想让你的函数能够访问一个外部变量,你必须在函数内部将其声明为 :global

function someFuntion(){
    global $myArr;
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;
}

有关详细信息,请参阅变量范围

但请注意,使用全局变量不是一个好的做法:这样一来,你的函数就不再是独立的了。


一个更好的主意是让你的函数返回结果

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 手册的“功能”部分,尤其是以下小节:

评论

3赞 Pascal MARTIN 3/28/2010
@Machine :^^是一个很好的问题(从那以后,我已经编辑了我的答案几次以添加更多信息;也许它被否决了,因为一开始不够完整......它可能与人们不喜欢的全球有关......
3赞 Tyler Carter 3/28/2010
@Machine Anti-Global @Coronatus 先生认为完全可行的答案是错误的。他的 1,662 次代表使他正确......
1赞 PatrikAkerstrand 3/28/2010
我读了你的初始版本,它仍然足够好,可以投 +1 票,绝对不值得投反对票。
1赞 Eugene Mala 11/4/2016
global方法引用到全局变量,而不是外部!
43赞 Maxwell s.c 2/24/2017 #5

您可以使用匿名函数

$foo = 42;
$bar = function($x = 0) use ($foo) {
    return $x + $foo;
};
var_dump($bar(10)); // int(52)

或者您可以使用箭头函数

$bar = fn($x = 0) => $x + $foo;
0赞 Vector-Meister 10/14/2017 #6

这真的是关于事物的正确顺序。

<?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);

?>

评论

1赞 robrecord 6/17/2020
有太多不相关的代码,很难在这里看到你提出的解决方案。