php php中的全局数组

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

global array in php

phparraysglobal-variables

提问by hd.

i have to function in two different files. one of them should add a new item to an array each time is called and the array should be accessible .what i did for it is :

我必须在两个不同的文件中运行。其中一个应该在每次调用时向数组添加一个新项目,并且该数组应该是可访问的。我为它所做的是:

function1(){

   global $array;

   $array[] = 'hi';

}

but it just create one item in array even if i call this function 4 times .

但即使我调用此函数 4 次,它也只会在数组中创建一项。

回答by Alin Purcaru

What you did should work.

你所做的应该有效。

<?php

function function1(){

   global $array;

   $array[] = 'hi';

}
function1();
function1();
function1();
print_r($array);

Test it.

测试一下。

You probably have another problem. Please note that the lifetime of all variables is the current run of your script. They won't exist in a successive run. For that you need to use some sort of persistence like session, cookie, file system, database.

你可能有另一个问题。请注意,所有变量的生命周期都是脚本的当前运行。它们不会在连续运行中存在。为此,您需要使用某种持久性,如会话、cookie、文件系统、数据库。

For more help post your complete code.

如需更多帮助,请发布您的完整代码。

回答by AgentConundrum

I'm a bit confused by the wording of your question. When you say "i have to function in two different files." does you mean you have "two" functions?

我对你问题的措辞有些困惑。当您说“我必须在两个不同的文件中运行”时。你的意思是你有“两个”功能?

If you have two functions both trying to use your $array variable, you'll need to call global $array;in both functions.

如果您有两个函数都试图使用 $array 变量,则需要同时调用global $array;这两个函数。

The reason for this is that globalis a bit misleading. All it's really doing is assigning a reference to a member of $_GLOBALSto a variable in the local scope which has the same name as the $_GLOBALS index. In other words, if you do something like this:

这样做的原因global是有点误导。它真正做的就是将一个成员的引用分配给$_GLOBALS局部作用域中与 $_GLOBALS 索引同名的变量。换句话说,如果你做这样的事情:

 global $variable;

it's essentially the same thing as saying this:

这与说这件事本质上是一样的:

 $variable =& $_GLOBALS['variable'];   (assign by reference)

The actual variable $variableis still scoped at the function level, it just happens to have a reference to a global variable.

实际变量$variable的作用域仍然在函数级别,它恰好有一个对全局变量的引用。

The implication of this is that if you don't define global $variablein every function, you're just creating a brand new variable within the scope of that function. When the function ends, the variable is unset and any changes made to it within the function are lost.

这意味着如果您没有global $variable在每个函数中定义,您只是在该函数的范围内创建了一个全新的变量。当函数结束时,变量将被取消设置,并且在函数内对其所做的任何更改都将丢失。

With all of that said, global variables still tend to be a bad idea. It's a lot clearer if you just maintain a local variable, and pass it as a parameter to other functions when needed.

尽管如此,全局变量仍然往往是一个坏主意。如果只维护一个局部变量,并在需要时将其作为参数传递给其他函数,那就更清楚了。