PHP $GLOBALS['variable']; 和有什么区别和全局$变量;
请看下面两个例子:
例子 1:
$variable = 'some value';
class Foo {
public function bar() {
global $variable;
print $variable;
}
}
例子 2:
$variable = 'some value';
class Foo {
public function bar() {
print $GLOBALS['variable'];
}
}
例子可以这样使用:
$foo = new Foo();
$foo->bar();
两者看起来都做了同样的事情?有什么区别?一种方法比另一种方法更好吗?为什么有两种不同的方法来做到这一点?
第一个示例对我来说似乎很奇怪,因为它看起来像是一个声明,然后使用变量而不分配它......对我来说看起来很奇怪。
谢谢。
Please look at the following two examples:
Example 1:
$variable = 'some value';
class Foo {
public function bar() {
global $variable;
print $variable;
}
}
Example 2:
$variable = 'some value';
class Foo {
public function bar() {
print $GLOBALS['variable'];
}
}
Examples can be used like this:
$foo = new Foo();
$foo->bar();
Both seem to do the same thing? What is the difference? Is one way better than the other? Why are there two different ways of doing this?
The first example seems strange to me because it looks like a declaration followed by using the variable without assigning it.. looks weird to me.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
global
关键字将变量“导入”到本地范围,而通过$GLOBALS
访问全局变量则不会。The
global
keyword 'imports' the variable into the local scope, while accessing a global through$GLOBALS
does not.$GLOBALS
只是访问全局变量的另一种方式。global
关键字将全局变量导入到局部作用域中。$GLOBALS
可用于访问全局变量,而不会污染局部作用域。例如,它可用于迭代全局变量。
请参阅
$GLOBALS
:并且
全局关键字
:$GLOBALS
is just an other way of accessing global variables. Theglobal
keywords imports global variables into the local scope.$GLOBALS
can be used to access global variables without polluting the local scope.It may be used to iterate over global variables, for example.
See
$GLOBALS
:And
the global keyword
:区别之一在于使用方式。从 手册页
可以看到可以访问这个全局变量和一个局部变量。使用
global $foo
时无法做到这一点。还有其他细微的差别,但我认为没什么重要的。您可能想问自己是否应该使用全局变量,就我而言,它的使用方式几乎总是一种反模式,尽管这可能是一种品味。
One difference is in use. From the manual page
You can see that you can access this, global variable and a local variable. You couldn't do that when useing
global $foo
. There are other subtile differences, but nothing that matter I think.You might want to ask yourself if you should be using the globals anyway, the way it is used is almost always an anti-pattern as far as i'm conserned, although this might be a taste-thing.
$GLOBALS
以Array
的形式表示整个全局变量表。global
关键字允许您指定一个或多个变量标签,这些标签是局部变量表中全局变量表的一部分。在其他答案中通常称为导入。曾经有一段时间,函数作用域内的
$GLOBALS
没有立即更新全局表。但我认为现在已经解决了。至少我无法再重现它了。所以区别仅仅在于如何编写某些内容的语义。
然而,两者之间是有区别的。当您通过
global
关键字在本地范围内访问全局变量时,您无法取消设置它们。但您可以通过
$GLOBALS
访问它们。这是非常具体的,如果有兴趣请参阅演示。
$GLOBALS
represents the whole global variable table in form of anArray
.The
global
keyword allows you to specify one or more variable labels, that are part of the global variable table within the local variable table. Often called import in the other answers.There once was a time when
$GLOBALS
within a function scope did not update the global table immediately. But I think that is fixed now. At least I was not able to reproduce it any longer.So the difference is merely the semantics how to write something.
However, there is a difference between the two. You can not unset global variables when you make them accessible within the local scope via the
global
keyword.But you can by accessing them via
$GLOBALS
.This is pretty specific, if interested see Demo.
来自 php 手册 http://php.net/manual/en/language.references。 Whatdo.php
From php manual http://php.net/manual/en/language.references.whatdo.php