从外部获取变量,在 PHP 中的函数内部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5060465/
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
Get variables from the outside, inside a function in PHP
提问by Henrikz
I'm trying to figure out how I can use a variable that has been set outside a function, inside a function. Is there any way of doing this? I've tried to set the variable to "global" but it doesn't seems to work out as expected.
我试图弄清楚如何使用在函数外部、函数内部设置的变量。有没有办法做到这一点?我试图将变量设置为“全局”,但它似乎没有按预期工作。
A simple example of my code
我的代码的一个简单示例
$var = '1';
function() {
$var + 1;
return $var;
}
I want this to return the value of 2.
我希望它返回 2 的值。
回答by Jesse Cohen
You'll need to use the global keyword insideyour function. http://php.net/manual/en/language.variables.scope.php
您需要在函数中使用 global 关键字。 http://php.net/manual/en/language.variables.scope.php
EDIT(embarrassed I overlooked this, thanks to the commenters)
编辑(很尴尬我忽略了这一点,感谢评论者)
...and store the result somewhere
...并将结果存储在某处
$var = '1';
function() {
global $var;
$var += 1; //are you sure you want to both change the value of $var
return $var; //and return the value?
}
回答by Jacob
Globals will do the trick but are generally good to stay away from. In larger programs you can't be certain of there behaviour because they can be changed anywhere in the entire program. And testing code that uses globals becomes very hard.
全局变量可以解决问题,但通常最好远离。在较大的程序中,您不能确定那里的行为,因为它们可以在整个程序的任何地方更改。测试使用全局变量的代码变得非常困难。
An alternative is to use a class.
另一种方法是使用类。
class Counter {
private $var = 1;
public function increment() {
$this->var++;
return $this->var;
}
}
$counter = new Counter();
$newvalue = $counter->increment();
回答by Czechnology
$var = 1;
function() {
global $var;
$var += 1;
return $var;
}
OR
或者
$var = 1;
function() {
$GLOBALS['var'] += 1;
return $GLOBALS['var'];
}
回答by Chamandeep
$var = '1';
function addOne() use($var) {
return $var + 1;
}
回答by Nathan Anderson
See http://php.net/manual/en/language.variables.scope.phpfor documentation. I think in your specific case you weren't getting results you want because you aren't assigning the $var + 1
operation to anything. The math is performed, and then thrown away, essentially. See below for a working example:
有关文档,请参阅http://php.net/manual/en/language.variables.scope.php。我认为在您的特定情况下,您没有得到想要的结果,因为您没有将$var + 1
操作分配给任何东西。数学被执行,然后被扔掉,本质上。请参阅下面的工作示例:
$var = '1';
function addOne() {
global $var;
$var = $var + 1;
return $var;
}
回答by Brian Driscoll
This line in your function:
$var + 1
will not change the value assigned to $var
, even if you use the global keyword.
函数中的这一行:
即使您使用 global 关键字,也$var + 1
不会更改分配给 的值$var
。
Either of these will work, however:
$var = $var + 1;
or
$var += 1;
然而,这些方法中的任何一个都可以:
$var = $var + 1;
或
$var += 1;
回答by Brian Driscoll
<?php
$var = '1';
function x ($var) {
return $var + 1;
}
echo x($var); //2
?>