PHP 全局或 $GLOBALS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3573847/
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-25 10:16:33  来源:igfitidea点击:

PHP global or $GLOBALS

phpvariablesfunctionglobal

提问by Francisc

Is there a best practice / recommendation when I want to use a variable declared outside of a function when it comes to using:

当我想使用在函数外部声明的变量时,是否有最佳实践/建议:

  1. global $myVar
  2. $GLOBALS['myVar']
  1. global $myVar
  2. $GLOBALS['myVar']

Thank you.

谢谢你。

采纳答案by reko_t

What you should really do is pass the variable to the function instead of using a global at all.

您真正应该做的是将变量传递给函数,而不是完全使用全局变量。

An example how to change a variable outside of the function via passing it as reference parameter:

如何通过将变量作为引用参数传递来更改函数外部的变量的示例:

function myFunc(&$myVar)
{
    $myVar = 10;
}

$foo = 0;
myFunc($foo);
var_dump($foo); // yields 10

回答by Artefacto

Well, you should only use globals in limited circumstances, but to answer your question:

好吧,您应该只在有限的情况下使用全局变量,但要回答您的问题:

  1. globalis potentially marginally faster (it will rarely make a difference).
  2. $GLOBALS(not $GLOBAL) is more readable, because every time you see it, you know you are accessing/changing a global variable. This can be crucial in avoiding nasty bugs.
  3. Inside the function, if you want to unset a global variable, you must use unset($GLOBALS['varname']), not global $varname; unset($varname);.
  1. global可能会稍微快一点(它很少会产生影响)。
  2. $GLOBALS(not $GLOBAL) 更具可读性,因为每次看到它时,您都知道您正在访问/更改全局变量。这对于避免讨厌的错误至关重要。
  3. 在函数内部,如果要取消设置全局变量,则必须使用unset($GLOBALS['varname']),而不是global $varname; unset($varname);

As to points 1 and 2, I'll quoteSara Golemon here:

关于第 1 点和第 2 点,我将在这里引用Sara Golemon:

What does that mean for your use of the $GLOBALSarray? That's right, the globalkeyword is technically faster. Now, I want to be really clear about one thing here. The minor speed affordance given by using your globals as localized [compiled variables] needs to be seriously weighed against the maintainability of looking at your code in five years and knowing that $foocame from the global scope. something_using($GLOBALS['foo']);will ALWAYS be clearer to you down the line than global $foo; /* buncha code */ something_using($foo);Don't be penny-wise and pound foolish..

这对您使用$GLOBALS数组意味着什么?没错,global关键字在技术上更快。现在,我想在这里澄清一件事。需要认真权衡使用全局变量作为本地化 [编译变量] 所带来的微小速度可供性与五年后查看代码并知道代码$foo来自全局范围的可维护性。something_using($GLOBALS['foo']);对你来说,永远比global $foo; /* buncha code */ something_using($foo);不要吝啬和愚蠢更清楚。

回答by Lekensteyn

Use globalat the top of your function. That way, you can easily see what globals are used.

global在函数的顶部使用。这样,您可以轻松查看使用了哪些全局变量。

回答by Mewp

global $var;is equivalent to $var =& $GLOBALS['var'].

global $var;相当于$var =& $GLOBALS['var']

Some people suggested that it is faster than using $GLOBALS, however it's not necessarily the case. If you use the variable only once, $GLOBALSwill be faster, because you won't waste time for assignment.

有些人认为它比 using 更快$GLOBALS,但事实并非如此。如果你只使用一次变量,$GLOBALS会更快,因为你不会浪费时间进行赋值。

However, if you do use the variable multiple times, using global(or the equivalent assignment) is faster, because search the array for the varkey only once.

但是,如果多次使用变量,使用global(或等效赋值)会更快,因为只在数组中搜索var键一次。

That's it about speed. However, the speed difference is really small, and readability is more important. However, different people have different preferences about readability -- I prefer global, some other people answering here prefer $GLOBALS, so it's up to you to decide what looks better.

这就是速度。但是,速度差异真的很小,可读性更重要。但是,不同的人对可读性有不同的偏好——我更喜欢global,在这里回答的其他一些人更喜欢$GLOBALS,所以由你决定什么看起来更好。