php 声明一个全局数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12876222/
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
Declaring a global array
提问by Nistor Alexandru
Hi. I recently learned PHP and am trying to declare a global array so I can access inside a function. But I seem to be missing something because I get the error 'Undefined variable:'
你好。我最近学习了 PHP 并试图声明一个全局数组,以便我可以在函数内部访问。但我似乎错过了一些东西,因为我收到错误“未定义变量:”
Here is my code:
这是我的代码:
global $second_array;
$second_array = array();
function operatii($v) {
$var1 = $second_array[count($second_array)-1];
$var2 = $second_array[count($second_array)-2];
$rez = null;
echo $var1 . $var2 . "este?";
}
for ($i = 0; $i < count($a); $i++){
if ($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ) {
operatii($a[$i]);
} else {
array_push($second_array, $a[$i]);
}
}
I seem to be able to use the $second_arrayin the for loop, but can't use it in the operatii function.
How can I solve this problem?
我好像可以$second_array在for循环中使用,但是不能在operatii函数中使用。
我怎么解决这个问题?
回答by Spudley
There are two ways to reference a global variable in PHP:
PHP 中引用全局变量有两种方式:
- Use the
globalkeyword at the start of every function that uses the variable. - Use the
$GLOBALSarray.
global在使用该变量的每个函数的开头使用关键字。- 使用
$GLOBALS数组。
Of these, I recommend sticking with the second, since it makes it absolutely clear at all times that the variable is a global.
其中,我建议坚持使用第二个,因为它在任何时候都清楚地表明该变量是一个全局变量。
One of the problems with globals is keeping track of where they're being used; using the $GLOBALSarray mitigates this issue to some extent.
全局变量的问题之一是跟踪它们的使用位置;使用$GLOBALS数组在一定程度上缓解了这个问题。
However, there are still issues with using globals; it's generally considered poor practice to use too many globals in your code. Having worked with a number of legacy systems that used globals extensively, I can vouch for the fact that they can cause a lot of headaches for future developers.
但是,使用全局变量仍然存在问题;在代码中使用太多全局变量通常被认为是不好的做法。在使用了大量广泛使用全局变量的遗留系统后,我可以保证它们会给未来的开发人员带来很多麻烦。
Using globals also makes it harder to write formal test suites for your code (ie unit tests).
使用全局变量也使得为您的代码编写正式的测试套件(即单元测试)变得更加困难。
My recommendation would therefore be to avoid using globals at all where possible. They are necessary in some cases, but the more you can avoid them, and instead pass variables into your functions and classes rather than making them global, the better things will be.
因此,我的建议是尽可能避免使用全局变量。在某些情况下它们是必要的,但是你越能避免它们,而是将变量传递到你的函数和类中而不是将它们设为全局,事情就会越好。
So to summarise:
所以总结一下:
If you must use globals, reference them with $GLOBALS['varname'], but it's usually better not to use them at all.
如果您必须使用全局变量,请使用 引用它们$GLOBALS['varname'],但通常最好根本不要使用它们。
Hope that helps.
希望有帮助。
回答by Alain Tiemblo
As about everybody suggested, you should pass your array as function parameter. Using globals is a bad practice in major cases.
至于建议的每个人,您应该将数组作为函数参数传递。在主要情况下,使用全局变量是一种不好的做法。
function operatii($second_array, $v) {
$var1 = $second_array[count($second_array)-1];
$var2 = $second_array[count($second_array)-2];
$rez = null;
echo $var1 . $var2 . "este?";
}
$second_array = array();
for ($i = 0; $i < count($a); $i++) {
if ($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ) {
operatii($second_array, $a[$i]);
} else {
array_push($second_array, $a[$i]);
}
}
回答by Neil Richins
For anyone else hitting this old question in a google search,
对于在谷歌搜索中遇到这个老问题的其他人,
In the example the variable $second_array was declared a global, not the array created in the following line. To avoid this make sure the global declaration comes after the array declaration. My preference is to put global declaration in the function itself.
在示例中,变量 $second_array 被声明为全局变量,而不是在下一行中创建的数组。为避免这种情况,请确保全局声明在数组声明之后。我的偏好是将全局声明放在函数本身中。
$second_array = array();
function operatii($v) {
global $second_array;
$var1 = $second_array[count($second_array)-1];
$var2 = $second_array[count($second_array)-2];
$rez = null;
echo $var1 . $var2 . "este?";
}
for ($i = 0; $i < count($a); $i++){
if ($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ) {
operatii($a[$i]);
} else {
array_push($second_array, $a[$i]);
}
}

