让我的函数访问外部变量

发布于 2025-01-10 09:27:59 字数 262 浏览 0 评论 0 原文

我在外面有一个数组:

$myArr = array();

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

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

如何为函数提供正确的变量作用域?

I have an array outside:

$myArr = array();

I would like to give my function access to the array outside it so it can add values to it

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

How do I give the function the right scoping to the variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

软甜啾 2025-01-17 09:27:59

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

如果您希望函数能够访问外部变量,则必须在函数内部将其声明为全局变量:

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 手册的 /a> 部分,特别是以下子部分:

By default, when you are inside a function, you do not have access to the outer variables.

If you want your function to have access to an outer variable, you have to declare it as global, inside the function :

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

For more informations, see Variable scope.

But note that using global variables is not a good practice : with this, your function is not independant anymore.

A better idea would be to make your function return the result :

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;
}

And call the function like this :

$result = someFunction();

Your function could also take parameters, and even work on a parameter passed by reference :

function someFuntion(array & $myArr){
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;      // Put that $myVal into the array
}

Then, call the function like this :

$myArr = array( ... );
someFunction($myArr);  // The function will receive $myArr, and modify it

With this :

  • Your function received the external array as a parameter
  • And can modify it, as it's passed by reference.
  • And it's better practice than using a global variable : your function is a unit, independant of any external code.

For more informations about that, you should read the Functions section of the PHP manual, and,, especially, the following sub-sections :

枕梦 2025-01-17 09:27:59

您可以使用匿名函数

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

或者您可以使用箭头函数

$bar = fn($x = 0) => $x + $foo;

You can use an anonymous function:

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

Or you can use an arrow function:

$bar = fn($x = 0) => $x + $foo;
演多会厌 2025-01-17 09:27:59
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);

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

Global $myArr;
$myArr = array();

function someFuntion(){
    global $myArr;

    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;
}

Be forewarned, generally people stick away from globals as it has some downsides.

You could try this

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

That would make it so you aren't relying on Globals.

随心而道 2025-01-17 09:27:59
$myArr = array();

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

    return $myArr;
}

$myArr = someFunction($myArr);
$myArr = array();

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

    return $myArr;
}

$myArr = someFunction($myArr);
兔小萌 2025-01-17 09:27:59

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

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

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

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

$myArr = someFunction($myArr);

The one and probably not so good way of achieving your goal would using global variables.

You could achieve that by adding global $myArr; to the beginning of your function.
However note that using global variables is in most cases a bad idea and probably avoidable.

The much better way would be passing your array as an argument to your function:

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

$myArr = someFunction($myArr);
断桥再见 2025-01-17 09:27:59

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

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

?>

It really is about the correct order of things.

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文