返回变量内函数的结果以更改变量

发布于 2025-01-10 13:10:14 字数 879 浏览 0 评论 0原文

我正在尝试将函数传递到现有变量中以在模板中设置特定位置。但是,当我使用功能开关时,我没有收到任何结果。

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

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

发布评论

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

评论(2

不可一世的女人 2025-01-17 13:10:14

我确实设法解决了这个问题。

${'omni'.posicionnavbar($tabs['position'])} .= $nuevo_tab;

感谢 PHP Cookbook

了解将变量链接在一起的正确符号,我完全摆脱了开关和功能。我的设置方式 ${'omni'.$tabs['position']} .= $nuevo_tab; 足以完成工作。

I did manage to solve this.

${'omni'.posicionnavbar($tabs['position'])} .= $nuevo_tab;

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.

冧九 2025-01-17 13:10:14

您还可以使用函数返回的键创建一个数组 $omni。然后迭代结果会更容易

 <?php
        

function posicionnavbar($posicion) {
    switch ($posicion) {
        case 'top':
            return 'top';
        case 'soc':
            return 'soc';
        case 'nav':
            return 'nav';
        case 'usr':
            return 'usr';
    }
}

$tabs['position'] = 'top';
$nuevo_tab = "Something";

$omni[posicionnavbar($tabs['position'])] .= $nuevo_tab;

//let's imagine you got the value of $nuevo_tab here (for simulate the loop)
$omni['soc'] .= "Something related with soc";
$omni['nav'] .= "Something related with nav";    
$omni['usr'] .= "Something related with usr";   

foreach ($omni as $key => $value) {
    echo "\n";
    echo "the key {$key} has the value -> {$value}";
}

无论如何,你的答案是有效的。我提出的这一方案只是为了方便。如果将来您想通过一个简单的循环访问似乎相关的变量

You can also create an array $omni with keys returned from your function. Then it will be easier to iterate over the results

 <?php
        

function posicionnavbar($posicion) {
    switch ($posicion) {
        case 'top':
            return 'top';
        case 'soc':
            return 'soc';
        case 'nav':
            return 'nav';
        case 'usr':
            return 'usr';
    }
}

$tabs['position'] = 'top';
$nuevo_tab = "Something";

$omni[posicionnavbar($tabs['position'])] .= $nuevo_tab;

//let's imagine you got the value of $nuevo_tab here (for simulate the loop)
$omni['soc'] .= "Something related with soc";
$omni['nav'] .= "Something related with nav";    
$omni['usr'] .= "Something related with usr";   

foreach ($omni as $key => $value) {
    echo "\n";
    echo "the key {$key} has the value -> {$value}";
}

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

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