本文介绍了在循环内将变量设置为全局范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在PHP中定义一些临时全局变量,名为
因为您使用'字符串
尝试
为您的目的
$ b $ pre $ for($ i = 1; $ i {
// acess as $ GlOBALS ['MyGlobalVar'。$ i]并做任何你想做的事
$ GLOBALS ['MyGlobalVar'。$ i] = null
}
I want to define a number of temporary global variables in PHP called $MyGlobalVar1, $MyGlobalVar2 ... , but the problem is that the keyword 'global' takes the variable name literally:
for ($i = 1; $i<= 10; $i++) { $var = '$MyGlobalVar'.$i; global $var; }
i.e. $var is now global.
Setting quotes will not work because 'global' expects '$' and will stop execution at the single quote:
for ($i = 1; $i<= 10; $i++) { $var = '$MyGlobalVar'.$i; global '$var'; }
How to set the variables to global scope? Thanks.
解决方案
since you using ' it will always be taken as string
Try$GLOBALS for your purpose
for ($i = 1; $i<= 10; $i++) { // acess as $GlOBALS['MyGlobalVar'.$i] and do whatever you want $GLOBALS['MyGlobalVar'.$i] = null }
这篇关于在循环内将变量设置为全局范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!