【问题标题】:Echo out html set number of times - php [duplicate]回显html设置次数-php [重复]
【发布时间】:2013-08-05 16:04:15
【问题描述】:

我正在创建一个相当复杂的 html <table> 布局,在这个早期阶段,复制和粘贴每个 <tr> 以生成虚拟内容非常耗时。

我的想法是指定一个虚拟 <tr>$var,然后使用如下函数输出 x 次:

$html = "<tr>//content</tr>";

function dummy_html($html, $times){

        $i = 0;
        for ($i <= $times) {
            echo $html;
            $i = $i++; 
        }
    }

    echo dummy_html($html, 5); 

但这会在for 行返回一个解析错误,知道为什么会这样吗?

【问题讨论】:

  • $i = $i++;应该是 $i++;
  • 你的函数调用也应该是 dummy_html($html, 5);代替 echo dummy_html($html, 5);
  • 执行正确的 for 循环(如下所示)或使用 while 循环(仅使用 while 代替 for,其余的保持不变)
  • 在问了 195 个问题后,您会发现,至少第 196 个问题会显示一些以前的研究成果

标签: php html for-loop html-table


【解决方案1】:

PHP 已经有函数了

echo str_repeat($html,5);

【讨论】:

    【解决方案2】:

    您的for 循环不正确。它应该是这样的:

    for( $i = 0; $i <= $times; $i++ ) {
       echo $html;
    }
    

    更新

    @Your Common Sense的解决方案比较好:str_repeat(http://php.net/manual/en/function.str-repeat.php)

    http://php.net/manual/en/control-structures.for.php

    【讨论】:

      【解决方案3】:

      for 应使用符号:for (set arguments, conditions, command to run at the end of the loop),因此应为:

      for($i = 0; $i &lt;= $times; $i++)

      另外,我建议使用str_repeat (http://php.net/manual/en/function.str-repeat.php)

      【讨论】:

        猜你喜欢
        • 2012-01-02
        • 1970-01-01
        • 1970-01-01
        • 2016-02-10
        • 1970-01-01
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多