返回变量内函数的结果以更改变量
我正在尝试将函数传递到现有变量中以在模板中设置特定位置。但是,当我使用功能开关时,我没有收到任何结果。
function posicionnavbar($posicion) {
switch ($posicion) {
case 'top':
return 'top';
case 'soc':
return 'soc';
case 'nav':
return 'nav';
case 'usr':
return 'usr';
}
}
以及我试图附加的变量。
$omni[posicionnavbar($tabs['position'])] .= $nuevo_tab;
更多信息:
$tabs['position']
是 - top、soc、nav 或 usr
$omni***
是我的模板中每个位置使用的变量。 IE: $omnitop
我已使用 $omni .= $nuevo_tab;
传递变量,并在模板中使用 $omni
来确认一切正常,成功了。这导致我的功能出现问题。
如何将函数传递到变量中以更改变量名称?
最终结果将输出以下内容之一:
$omnitop .= $nuevo_tab;
$omnisoc .= $nuevo_tab;
$omninav .= $nuevo_tab;
$omniusr .= $nuevo_tab;
I am trying to pass a function into an existing variable to set specific locations within my template. However when I use the function switch I am not receiving any results.
function posicionnavbar($posicion) {
switch ($posicion) {
case 'top':
return 'top';
case 'soc':
return 'soc';
case 'nav':
return 'nav';
case 'usr':
return 'usr';
}
}
And the variable I am attempting to append to.
$omni[posicionnavbar($tabs['position'])] .= $nuevo_tab;
More Info:
$tabs['position']
Is either - top, soc, nav, or usr
$omni***
Is the variable used within my template for each location. IE: $omnitop
I have passed the variable using $omni .= $nuevo_tab;
and using $omni
within my template to confirm everything was working, with success. Which lead me to something being off with my function.
How can I pass my function into my variable to change the variable name?
end results would output one of the following:
$omnitop .= $nuevo_tab;
$omnisoc .= $nuevo_tab;
$omninav .= $nuevo_tab;
$omniusr .= $nuevo_tab;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确实设法解决了这个问题。
感谢 PHP Cookbook
了解将变量链接在一起的正确符号,我完全摆脱了开关和功能。我的设置方式
${'omni'.$tabs['position']} .= $nuevo_tab;
足以完成工作。I did manage to solve this.
thanks to PHP Cookbook
Knowing the proper symbols to link the variables together, I got rid of the switch and function altogether. The way I have this set up
${'omni'.$tabs['position']} .= $nuevo_tab;
is enough to get the job done.您还可以使用函数返回的键创建一个数组 $omni。然后迭代结果会更容易
无论如何,你的答案是有效的。我提出的这一方案只是为了方便。如果将来您想通过一个简单的循环访问似乎相关的变量
You can also create an array $omni with keys returned from your function. Then it will be easier to iterate over the results
Anyway, your answer is valid. The one I have proposed is only for convenience. In case in the future you want to access what seem to be variables that are related, with a simple loop