PHP-使用STR_PAD创建表

PHP-使用STR_PAD创建表,php,html-table,Php,Html Table,像这样的php示例 对于($i=1;$i请尝试类似的操作。使用模数%,您可以检查值是否为偶数/奇数,并打开/关闭表格单元格 //open the table echo '<table><tr>'; for ($i=1; $i<=6; $i++){ // if odd start cell if($i % 2 != 0) echo '<td>'; $i = str_pad($i, 2, "0", STR_PA

像这样的php示例


对于($i=1;$i请尝试类似的操作。使用模数
%
,您可以检查值是否为偶数/奇数,并打开/关闭表格单元格

//open the table
echo '<table><tr>';

 for ($i=1; $i<=6; $i++){

    // if odd start cell
    if($i % 2 != 0) echo '<td>';

            $i = str_pad($i, 2, "0", STR_PAD_LEFT);
            $url = $i."<br />";
            echo $url;

    //if even close the cell
    if($i % 2 == 0) echo '</td>';
        }

// Close the table      
echo '</tr></table>';

如果你知道你总是成对输出

# i personally prefer sprintf over str_pad.  But if you don't,
# you only have one place to have to change it.
$format = function($x) { return sprintf('%02d', $x); };

# write out each pair.  Note the $i+=2.
for ($i=1; $i<=6; $i+=2) {
    $first = $format($i);
    $second = $format($i+1);
    echo "<td>{$first}<br>{$second}<br></td>";
}
#我个人更喜欢sprint而不是stru-pad。但是如果你不喜欢,
#你只有一个地方需要改变它。
$format=函数($x){返回sprintf(“%02d”,$x);};
#写出每一对。注意$i+=2。

对于($i=1;$i这是我见过的最美的东西;谢谢分享!如果我像这样更改输出:
01
02
/--49
50
51
52
/--99
100
,我更改了什么?那么你希望每50次更改一次单元格?如果是这样,我已经用
$i%50==1
/
# i personally prefer sprintf over str_pad.  But if you don't,
# you only have one place to have to change it.
$format = function($x) { return sprintf('%02d', $x); };

# write out each pair.  Note the $i+=2.
for ($i=1; $i<=6; $i+=2) {
    $first = $format($i);
    $second = $format($i+1);
    echo "<td>{$first}<br>{$second}<br></td>";
}