PHP 函数使用外部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11086773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:43:26  来源:igfitidea点击:

PHP function use variable from outside

phpfunctionvariables

提问by RIK

function parts($part) { 
    $structure = 'http://' . $site_url . 'content/'; 
    echo($tructure . $part . '.php'); 
}

This function uses a variable $site_urlthat was defined at the top of this page, but this variable is not being passed into the function.

此函数使用$site_url在此页面顶部定义的变量,但未将此变量传递到函数中。

How do we get it to return in the function?

我们如何让它在函数中返回?

回答by Zbigniew

Add second parameter

添加第二个参数

You need to pass additional parameter to your function:

您需要将附加参数传递给您的函数:

function parts($site_url, $part) { 
    $structure = 'http://' . $site_url . 'content/'; 
    echo $structure . $part . '.php'; 
}

In case of closures

在关闭的情况下

If you'd rather use closures then you can import variable to the current scope (the usekeyword):

如果您更愿意使用闭包,那么您可以将变量导入当前范围(use关键字):

$parts = function($part) use ($site_url) { 
    $structure = 'http://' . $site_url . 'content/'; 
    echo $structure . $part . '.php'; 
};

global- a bad practice

global- 不好的做法

This post is frequently read, so something needs to be clarified about global. Using it is considered a bad practice (refer to thisand this).

这篇文章经常被阅读,所以需要澄清一些关于global. 使用它被认为是一种不好的做法(请参阅thisthis)。

For the completeness sake here is the solution using global:

为了完整起见,这里是使用的解决方案global

function parts($part) { 
    global $site_url;
    $structure = 'http://' . $site_url . 'content/'; 
    echo($structure . $part . '.php'); 
}

It works because you have to tell interpreter that you want to use a global variable, now it thinksit's a local variable (within your function).

它起作用是因为你必须告诉解释器你想使用一个全局变量,现在它认为它是一个局部变量(在你的函数内)。

Suggested reading:

推荐阅读:

回答by Joe Green

Alternatively, you can bring variables in from the outside scope by using closures with the usekeyword.

或者,您可以通过使用带有use关键字的闭包从外部范围引入变量。

$myVar = "foo";
$myFunction = function($arg1, $arg2) use ($myVar)
{
 return $arg1 . $myVar . $arg2;
};

回答by Valentine Shi

Do not forget that you also can pass these usevariables by reference.

不要忘记您也可以通过use引用传递这些变量。

The use cases are when you need to change the use'd variable from inside of your callback (e.g. produce the new array of different objects from some source array of objects).

用例是当您需要use从回调内部更改'd 变量时(例如,从某些源对象数组生成不同对象的新数组)。

$sourcearray = [ (object) ['a' => 1], (object) ['a' => 2]];
$newarray = [];
array_walk($sourcearray, function ($item) use (&$newarray) {
    $newarray[] = (object) ['times2' => $item->a * 2];
});
var_dump($newarray);

Now $newarraywill comprise (pseudocode here for brevity)[{times2:2},{times2:4}].

现在$newarray将包括(为简洁起见,此处使用伪代码)[{times2:2},{times2:4}]

On the contrary, using $newarraywith no &modifier would make outer $newarrayvariable be read-only accessible from within the closure scope. But $newarraywithin closure scope would be a completelly different newly created variable living only within the closure scope.

相反,使用$newarray没有&修改将使外部$newarray变量是只读的,从封闭范围内的访问。但是$newarray在闭包范围内将是一个完全不同的新创建的变量,它只存在于闭包范围内。

Despite both variables' names are the same these would be two different variables. The outer $newarrayvariable would comprise []in this case after the code has finishes.

尽管两个变量的名称相同,但它们将是两个不同的变量。在这种情况下,外部$newarray变量将[]在代码完成后包含。

回答by HBv6

Just put in the function using GLOBAL keyword:

只需使用 GLOBAL 关键字放入函数:

 global $site_url;