PHP $GLOBALS['variable']; 和有什么区别和全局$变量;

发布于 2024-12-04 11:35:57 字数 552 浏览 2 评论 0原文

请看下面两个例子:

例子 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

书信已泛黄 2024-12-11 11:35:57

global 关键字将变量“导入”到本地范围,而通过 $GLOBALS 访问全局变量则不会。

The global keyword 'imports' the variable into the local scope, while accessing a global through $GLOBALS does not.

北城孤痞 2024-12-11 11:35:57

$GLOBALS 只是访问全局变量的另一种方式。 global 关键字将全局变量导入到局部作用域中。 $GLOBALS 可用于访问全局变量,而不会污染局部作用域。

例如,它可用于迭代全局变量。

请参阅$GLOBALS

$GLOBALS — 引用全局范围内所有可用的变量

并且 全局关键字

通过在函数内声明 $a 和 $b 全局,对任一变量的所有引用都将引用全局版本。

从全局范围访问变量的第二种方法是使用特殊的 PHP 定义的 $GLOBALS 数组。

$GLOBALS is just an other way of accessing global variables. The global 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:

$GLOBALS — References all variables available in global scope

And the global keyword:

By declaring $a and $b global within the function, all references to either variable will refer to the global version.

A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.

动次打次papapa 2024-12-11 11:35:57

区别之一在于使用方式。从 手册页

<?php
function test() {
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example content";
test();
?>

可以看到可以访问这个全局变量和一个局部变量。使用 global $foo 时无法做到这一点。还有其他细微的差别,但我认为没什么重要的。

您可能想问自己是否应该使用全局变量,就我而言,它的使用方式几乎总是一种反模式,尽管这可能是一种品味。

One difference is in use. From the manual page

<?php
function test() {
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example content";
test();
?>

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.

放低过去 2024-12-11 11:35:57

$GLOBALSArray 的形式表示整个全局变量表。

global 关键字允许您指定一个或多个变量标签,这些标签是局部变量表中全局变量表的一部分。在其他答案中通常称为导入

曾经有一段时间,函数作用域内的 $GLOBALS 没有立即更新全局表。但我认为现在已经解决了。至少我无法再重现它了。

所以区别仅仅在于如何编写某些内容的语义。

然而,两者之间是有区别的。当您通过 global 关键字在本地范围内访问全局变量时,您无法取消设置它们。

$var = 1;
foo();

function foo()
{
    global $var;
    unset($var); # won't unset the global variable
}

但您可以通过 $GLOBALS 访问它们。

$var = 1;
foo();

function foo()
{
    unset($GLOBALS['var']); # unsets the global variable
}

这是非常具体的,如果有兴趣请参阅演示

$GLOBALS represents the whole global variable table in form of an Array.

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.

$var = 1;
foo();

function foo()
{
    global $var;
    unset($var); # won't unset the global variable
}

But you can by accessing them via $GLOBALS.

$var = 1;
foo();

function foo()
{
    unset($GLOBALS['var']); # unsets the global variable
}

This is pretty specific, if interested see Demo.

み格子的夏天 2024-12-11 11:35:57

来自 php 手册 http://php.net/manual/en/language.references。 Whatdo.php

考虑全局 $var;作为 $var =& 的快捷方式$GLOBALS['var'];

From php manual http://php.net/manual/en/language.references.whatdo.php

Think about global $var; as a shortcut to $var =& $GLOBALS['var'];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文