在 PHP 函数中访问全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15687363/
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
Access a global variable in a PHP function
提问by Amin Gholibeigian
According to the most programming languages scope rules, I can access variables that are defined outside of functions inside them, but why doesn't this code work?
根据大多数编程语言的范围规则,我可以访问在它们内部函数之外定义的变量,但为什么这段代码不起作用?
<?php
$data = 'My data';
function menugen() {
echo "[" . $data . "]";
}
menugen();
?>
The output is []
.
输出是[]
。
回答by Matteo Tassinari
It is not working because you haveto declare which global variables you'll be accessing:
它不起作用,因为您必须声明您将访问哪些全局变量:
$data = 'My data';
function menugen() {
global $data; // <-- Add this line
echo "[" . $data . "]";
}
menugen();
Otherwise you can access it as $GLOBALS['data']
. See Variable scope.
否则,您可以将其作为$GLOBALS['data']
. 请参阅变量范围。
Even if a little off-topic, I'd suggest you avoid using globals at all and prefer passing as parameters.
即使有点跑题,我还是建议您完全避免使用全局变量,而更喜欢将其作为参数传递。
回答by jcbwlkr
You can do one of the following:
您可以执行以下操作之一:
<?php
$data = 'My data';
function menugen() {
global $data;
echo "[" . $data . "]";
}
menugen();
Or
或者
<?php
$data = 'My data';
function menugen() {
echo "[" . $GLOBALS['data'] . "]";
}
menugen();
That being said, overuse of globals can lead to some poor code. It is usually better to pass in what you need. For example, instead of referencing a global database object you should pass in a handle to the database and act upon that. This is called dependency injection. It makes your life a lot easier when you implement automated testing (which you should).
话虽如此,过度使用全局变量会导致一些糟糕的代码。通常最好传递您需要的内容。例如,您应该将句柄传递给数据库并对其进行操作,而不是引用全局数据库对象。这称为依赖注入。当您实施自动化测试(您应该这样做)时,它会让您的生活变得更加轻松。
回答by webnoob
It's a matter of scope. In short, global variables should be avoided so:
这是一个范围问题。简而言之,应避免使用全局变量,因此:
You either need to pass it as a parameter:
您要么需要将其作为参数传递:
$data = 'My data';
function menugen($data)
{
echo $data;
}
Orhave it in a class and access it
或者把它放在一个班级中并访问它
class MyClass
{
private $data = "";
function menugen()
{
echo this->data;
}
}
See @MatteoTassinari answer as well, as you can mark it as global to access it, but global variables are generally not required, so it would be wise to re-think your coding.
也请参阅@MatteoTassinari 答案,因为您可以将其标记为全局以访问它,但通常不需要全局变量,因此重新考虑您的编码是明智的。
回答by donvercety
Another way to do it:
另一种方法:
<?php
$data = 'My data';
$menugen = function() use ($data) {
echo "[".$data."]";
};
$menugen();
UPDATE 2020-01-13:requested by Peter Mortensen
更新 2020-01-13:Peter Mortensen 请求
As of PHP 5.3.0 we have anonymous functions support that can create closures. A closure can access the variable which is created outside of its scope.
从 PHP 5.3.0 开始,我们支持可以创建闭包的匿名函数。闭包可以访问在其作用域之外创建的变量。
In the example, the closure is able to access $data
because it was declared in the use
clause.
在示例中,闭包能够访问,$data
因为它是在use
子句中声明的。
回答by Psalms Kalu
For many years I have always used this format:
多年来我一直使用这种格式:
<?php
$data = "Hello";
function sayHello(){
echo $GLOBALS["data"];
}
sayHello();
?>
I find it straightforward and easy to follow. The $GLOBALS is how PHP lets you reference a global variable. If you have used things like $_SERVER, $_POST, etc. then you have reference a global variable without knowing it.
我觉得它简单易懂。$GLOBALS 是 PHP 允许您引用全局变量的方式。如果您使用过诸如 $_SERVER、$_POST 之类的东西,那么您就在不知道的情况下引用了一个全局变量。
回答by Sanjeev Budha
<?php
$data = 'My data';
$menugen = function() use ($data) {
echo "[ $data ]";
};
$menugen();
?>
You can also simplify
你也可以简化
echo "[" . $data . "]"
to
到
echo "[$data]"
回答by Major Productions
You need to pass the variable into the function:
您需要将变量传递给函数:
$data = 'My data';
function menugen($data)
{
echo $data;
}
回答by Mohamad Hamouday
If you want, you can use the "define" function, but this function creates a constant which can't be changed once defined.
如果需要,您可以使用“define”函数,但该函数会创建一个常量,一旦定义就无法更改。
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>