【问题标题】:How to Display my output in Php with return in a loop? [duplicate]如何在循环中显示我的输出并在 PHP 中返回? [复制]
【发布时间】:2019-11-01 16:24:36
【问题描述】:

我想输出我的代码。当我使用 return 时,它会在 1 次后分解并

(当我使用echo时,我的内容将显示在页面顶部。我认为我不应该在这里使用echo,因为我想稍后在循环中调用函数)

以下是展示其外观的图片:

我的返回代码:

function allmyshortcodesloopfunction() {
    $alltheshortcodes = '';
    $alltheshortcodes = 'thistextshouldbehere4times';

    for($i=0; $i < 4; $i++) {
        echo '<p></p>';
        return $alltheshortcodes;
    }
}

这看起来如何: enter image description here

echo 的样子:enter image description here

也许你可以发布一个我找不到的主题来解决这个问题。 未来有这个问题的人的一些标签。他们可以找到这个: 循环、while、if、return、statement、content、breaks,一次,一次

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    return 退出函数,你不能让它在循环中。单独存放,然后复用函数。

    function allmyshortcodesloopfunction() {
        $output = '';
        $alltheshortcodes = 'thistextshouldbehere4times';
    
        for ($i=0; $i < 4; $i++) {
            $output .= "<p>$alltheshortcodes</p>";
        }
    
        return $output;
     }
    

    【讨论】:

    • 谢谢。我做了一点不同:function allmyshortcodesloopfunction() { $alltheshortcodes = ''; $alltheshortcodes = 'thistextshouldbehere4times'; for($i=0; $i hi'; } 返回 $ 输出; }
    【解决方案2】:
    function allmyshortcodesloopfunction() {
        $alltheshortcodes = 'thistextshouldbehere4times';
        for ($i = 0; $i < 4; $i++) {
            echo "<p>$alltheshortcodes</p>";
        }
    }
    allmyshortcodesloopfunction();
    

    【讨论】:

    • 纯代码答案在 stackoverflow 上的价值很低,因为它们对教育/授权成千上万的未来研究人员几乎没有作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 2020-08-05
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多